DEV Community

Cover image for Build a Dart ADK Agent and MCP Server
xbill for Google Developer Experts

Posted on

Build a Dart ADK Agent and MCP Server

Build a Dart ADK Agent and MCP Server

Dart developers do not need a Python or Node.js service just to experiment with agents and Model Context Protocol (MCP) tools. This project uses adk_dart for the agent and shelf for a small HTTP server that exposes an MCP-compatible greeting tool over Server-Sent Events (SSE).

The complete code is in the adk-hello-world-dart repository.

Note- the Dart library is not an official ADK. An official Dart ADK has not been released as of July 2026. This approach provides an alternative to start working with agents in Dart without waiting for an official SDK.

What the sample contains

The repository has two related examples:

  • bin/main.dart creates an LlmAgent and registers an ADK FunctionTool.
  • bin/server.dart starts a Shelf server with an SSE endpoint and a JSON-RPC message endpoint.

The server implements the MCP methods needed by this demo: initialize, notifications/initialized, ping, tools/list, and tools/call. The transport and JSON-RPC routing are deliberately small and live in SessionService; they are not a general-purpose MCP server implementation.

flowchart LR
    Client[MCP client] -->|GET /sse| Server[Shelf server]
    Server -->|endpoint event| Client
    Client -->|POST /messages?sessionId=...| Server
    Server --> Session[SessionService]
    Session --> Tool[greet tool]

    CLI[Dart CLI] --> Agent[ADK LlmAgent]
    Agent --> ADKTool[ADK FunctionTool]
Enter fullscreen mode Exit fullscreen mode

1. Add the dependencies

The current project targets Dart 3.5 or later and uses these package versions:

environment:
  sdk: ^3.5.0

dependencies:
  adk_dart: ^2026.7.24
  adk_mcp: ^2026.7.24
  logging: ^1.3.0
  shelf: ^1.4.1
  shelf_router: ^1.1.4
  uuid: ^4.5.1
Enter fullscreen mode Exit fullscreen mode

Install them with:

dart pub get
Enter fullscreen mode Exit fullscreen mode

adk_dart is used directly by the sample agent. The repository also tracks adk_mcp, while the current server keeps its MCP transport explicit in SessionService so the protocol flow is easy to inspect.

2. Define the greeting tool

The project keeps the greeting logic separate from its ADK and MCP wrappers:

class Tools {
  static String formatGreeting(String name) {
    return 'Hello, $name!';
  }

  static final FunctionTool greetFunctionTool = FunctionTool(
    name: Config.toolGreet,
    description: 'Get a greeting from a local HTTPS server.',
    func: ({String? param}) {
      final name = param ?? 'World';
      return formatGreeting(name);
    },
  );
}
Enter fullscreen mode Exit fullscreen mode

Tools also exposes an MCP tool definition with a JSON Schema input named param. Keeping formatGreeting as a plain Dart function makes the domain behavior easy to unit test.

3. Create the ADK agent

AdkGreetingAgent attaches the function tool to an LlmAgent:

class AdkGreetingAgent {
  static LlmAgent createAgent() {
    return LlmAgent(
      name: 'GreetingAgent',
      description: 'An AI Agent built with adk_dart that provides greetings.',
      instruction:
          'You are a friendly greeting assistant. '
          'Use the greet tool to provide personalized greetings.',
      tools: [Tools.greetFunctionTool],
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Run the CLI example to confirm that the agent and tool can be created:

dart run bin/main.dart
Enter fullscreen mode Exit fullscreen mode

This command initializes the agent and prints a sample tool result. It does not call a hosted model.

4. Expose the MCP endpoints

The Shelf server registers four routes:

router.get('/', (request) => Response.ok('ADK & MCP Dart Server Running'));
router.get('/health', (request) => Response.ok('OK'));
router.get(Config.sseEndpoint, sessionService.handleSseSession);
router.post(Config.messagesEndpoint, sessionService.handlePostMessage);
Enter fullscreen mode Exit fullscreen mode

When a client opens GET /sse, SessionService creates an in-memory session and sends an endpoint event containing a URL such as:

/messages?sessionId=7c6d...
Enter fullscreen mode Exit fullscreen mode

The client posts JSON-RPC requests to that URL. Responses arrive as message events on the original SSE connection.

Start the server with:

dart run bin/server.dart
Enter fullscreen mode Exit fullscreen mode

It listens on port 8080 by default. Set the PORT environment variable to use another port.

5. Connect an MCP client

For an MCP client that supports remote SSE servers, point it at:

http://localhost:8080/sse
Enter fullscreen mode Exit fullscreen mode

A typical client configuration looks like this:

{
  "mcpServers": {
    "dart-greeting-server": {
      "url": "http://localhost:8080/sse"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Configuration keys differ between MCP clients, so check the documentation for the client you use. Once connected, call the greet tool with:

{
  "param": "Dart developer"
}
Enter fullscreen mode Exit fullscreen mode

The result is Hello, Dart developer!.

6. Test and verify the project

The repository includes unit tests for the greeting behavior and endpoint tests for the Shelf server:

dart test
dart analyze
Enter fullscreen mode Exit fullscreen mode

You can run the complete build, analysis, and test sequence with:

make check
Enter fullscreen mode Exit fullscreen mode

7. Build the container

The included multi-stage Dockerfile compiles the server to a native executable and copies it into a small scratch image:

docker build -t adk-hello-world-dart .
docker run --rm -p 8080:8080 adk-hello-world-dart
Enter fullscreen mode Exit fullscreen mode

Check the running container at http://localhost:8080/health.

8. Deploy to Cloud Run

cloudbuild.yaml builds the image, pushes it to Container Registry, and deploys the service in us-central1:

make deploy
Enter fullscreen mode Exit fullscreen mode

The deployment allows unauthenticated access and sets --max-instances 1.

That instance limit matters here. Active SSE transports are stored in a process-local map, so a POST request routed to another instance would not find its session. A production service should move session state to shared storage or use a transport and deployment design that does not depend on process-local routing. Authentication, origin restrictions, request validation, timeouts, and rate limiting would also need attention before exposing the service publicly.

Where to go next

This sample is intentionally narrow: one agent, one deterministic tool, and enough MCP handling to show the request flow. Useful next steps include replacing the greeting with real domain logic, using adk_mcp transport primitives as the Dart package evolves, adding model configuration to execute the agent, and moving session state out of memory before scaling the service.

Resources:

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev

The useful thing about pairing an agent with MCP is that it forces the integration boundary to become explicit. The server is not just plumbing; it is where permissions, tool shape, failure behavior, and auditability can live outside the agent prompt. That separation pays off quickly.