Skip to content

Client ​

Category: Mcp_client

Source: client.dart

MCP JSON-RPC client.

Speaks MCP over an injectable [JsonRpcTransport] from glue_server so the same client works against stdio, HTTP+SSE, WebSocket, or an in-memory transport in tests.

Responsibilities: • initialize handshake + protocol version negotiation • tools/list, tools/call with a concurrent-id pending map • rate-limit retry-once • notifications fan-out (tools/list_changed, future server events) • drop detection → pending calls fail with retryable metadata

Classes ​

McpNotification ​

A peer-side notification flowing from the server. tools/list_changed is the only one we currently care about, but we surface everything so callers can route as they like.

Constructor ​

dart
const McpNotification({required this.method, this.params})

Properties ​

PropertyTypeDescription
methodString
paramsMap<String, dynamic>?

McpCallFailure ​

Failure shape thrown by [McpClient.callTool] / [listTools] / [initialize] when the call cannot complete. The agent loop maps this onto ToolResult(success: false, ...).

Constructor ​

dart
const McpCallFailure({
    required this.reason,
    this.code,
    this.message,
    this.retryable = false,
    this.wwwAuthenticate,
  })

Properties ​

PropertyTypeDescription
reasonStringShort machine reason. Stable. Drives metadata.
codeint?JSON-RPC error code if this came from the server.
messageString?Human-readable message.
retryableboolWhether the agent loop may retry the same call.
wwwAuthenticateString?Raw WWW-Authenticate header, when this failure originated from a 401 transport error. Consumed by the pool to drive RFC 9728 discovery and refresh-token grant.

Methods ​

String toString() ​

McpClient ​

Constructor ​

dart
McpClient({
    required this.transport,
    this.clientInfo = const McpClientInfo(name: 'glue', version: '0.0.0'),
    this.clientCapabilities = const McpClientCapabilities(
      roots: McpRootsCapability(listChanged: true),
    ),
    this.callTimeout = const Duration(seconds: 30),
  })

Properties ​

PropertyTypeDescription
transportJsonRpcTransport
clientInfoMcpClientInfo
clientCapabilitiesMcpClientCapabilities
callTimeoutDuration
notificationsStream<McpNotification> get
initResultMcpInitializeResult? getSet after a successful [initialize]. Null otherwise.
notificationsStream<McpNotification> get
initResultMcpInitializeResult? getSet after a successful [initialize]. Null otherwise.

Methods ​

Future<McpInitializeResult> initialize() ​

Sends initialize + notifications/initialized. Negotiates the protocol version per the design doc: equal → continue; older within minimum-supported → continue with downgrade; newer → continue, upgrade-tolerant; older than minimum → refuse.

Future<List<McpToolDescriptor>> listTools() ​
`Future<McpToolCallResult> callTool( ​
String name,
Map&lt;String, dynamic&gt; arguments,

)`

Calls a tool. On JSON-RPC rate-limit (-32011) we honour the hint and retry once. On transport drop, all pending calls (including this one) resolve with [McpCallFailure(retryable: true)].

Future&lt;void&gt; close() ​

Released under the MIT License.