agentic_mcp library
Model Context Protocol for the agentic framework.
Two directions, one vocabulary. McpClient turns a remote server's tools
into ordinary Tool objects that every agent in the framework already knows
how to run; McpServer publishes your own ToolRegistry to any MCP client.
import 'package:agentic_mcp/agentic_mcp.dart';
final client = McpClient(
transport: McpHttpTransport(endpoint: Uri.parse('https://…/mcp')),
);
await client.initialize();
await registerMcpTools(client, registry, prefix: 'fs');
// The agent now has the server's tools, and knows nothing about MCP.
The subprocess transport lives in package:agentic_mcp/io.dart, because it
imports dart:io and this library must stay usable on the web.
Classes
- InMemoryTransport
- A transport whose two halves are wired directly together.
- JsonRpcError
- A JSON-RPC failure.
- JsonRpcErrorCode
- Error codes defined by JSON-RPC 2.0 itself.
- JsonRpcMessage
- Classifies a decoded JSON-RPC message.
- JsonRpcNotification
- A message that expects no response.
- JsonRpcNotificationMessage
- An incoming notification.
- JsonRpcRequest
- A call that expects a response.
- JsonRpcRequestMessage
- An incoming request.
- JsonRpcResponse
- What a request returned.
- JsonRpcResponseMessage
- An incoming response.
- McpCapabilities
- What a peer says it can do.
- McpClient
- A session with one MCP server.
- McpEvent
- Base for every MCP event.
- McpHttpTransport
- Talks to an MCP server over Streamable HTTP.
- McpImplementation
- Who is speaking, and what version of it.
- McpMethod
- Every method name in the protocol.
- McpNotificationReceived
- The server sent a notification.
- McpPrompt
- A prompt template a server offers.
- McpPromptArgument
- One argument of a prompt template.
- McpPromptResult
- A rendered prompt template.
- McpResource
- Something a server can be asked to read.
- McpResourceContents
- The contents of one resource.
- McpResourceTemplate
- A family of resources addressed by a URI template.
- McpServer
-
Serves a
ToolRegistryover the Model Context Protocol. - McpSessionInfo
- What the server said when the session opened.
- McpSessionOpened
- A session was negotiated.
- McpTool
- A tool that runs on an MCP server.
- McpToolAnnotations
- Hints a server gives about how a tool behaves.
- McpToolCalled
- A tool call on a server finished.
- McpToolCallResult
- What a tool call returned.
- McpToolDescriptor
- A tool as the server describes it.
- McpToolsDiscovered
- Tools were discovered from a server.
- McpTransport
- Carries JSON-RPC messages to and from a peer.
Constants
- kJsonRpcVersion → const String
- The only JSON-RPC version MCP speaks.
- kLatestProtocolVersion → const String
- The revision this package proposes.
- kProtocolVersionHeader → const String
- Header carrying the negotiated protocol revision.
-
kReservedHeaders
→ const Set<
String> -
The default
initializeheaders a client sends. - kSessionIdHeader → const String
- Header carrying the session identifier.
-
kSupportedProtocolVersions
→ const List<
String> - Protocol revisions this package can speak, newest first.
Functions
-
contentPartFromMcp(
JsonMap block) → ContentPart? -
Converts one MCP content block into a
ContentPart. -
contentPartsFromMcp(
Object? blocks) → List< ContentPart> - Converts a list of MCP content blocks.
-
contentPartsToMcp(
Iterable< ContentPart> parts) → List<Object?> - Converts a list of content parts.
-
contentPartToMcp(
ContentPart part) → JsonMap? -
Converts a
ContentPartinto an MCP content block. -
isSupportedProtocolVersion(
String version) → bool -
Whether
versionis one this package can speak. -
mcpTools(
McpClient client, {String? prefix, Set< String> include = const <String>{}, Set<String> exclude = const <String>{}, Set<String> approvalRequired = const <String>{}, Duration? timeout, AgenticContext? context}) → Future<List< McpTool> > - Discovers a server's tools and wraps each one.
-
negotiateProtocolVersion(
String serverVersion) → String? - Picks the newest revision both sides understand.
-
registerMcpTools(
McpClient client, ToolRegistry registry, {String? prefix, Set< String> include = const <String>{}, Set<String> exclude = const <String>{}, Set<String> approvalRequired = const <String>{}, Duration? timeout, bool replace = false, AgenticContext? context}) → Future<List< String> > - Discovers and registers a server's tools in one step.
-
renderMcpContent(
Object? blocks) → String - Renders MCP content blocks as the plain text a model reads.
Typedefs
-
McpRequestHandler
= Future<
JsonMap> Function(JsonRpcRequest request, AgenticContext context) - Handles a request the server sent to the client.
-
McpResourceProvider
= Future<
List< Function(AgenticContext context)JsonMap> > - Supplies the resources a server offers.
-
McpResourceReader
= Future<
List< Function(String uri, AgenticContext context)JsonMap> ?> -
Reads one resource by URI, or returns
nullif it does not exist.