Client class
Main MCP Client class that handles all client-side protocol operations
- Available extensions
Constructors
- Client({required String name, required String version, ClientCapabilities capabilities = const ClientCapabilities()})
- Creates a new MCP client with the specified parameters
Properties
- capabilities → ClientCapabilities
-
Client capabilities configuration
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- isConnected → bool
-
Whether the client is currently connected
no setter
- name → String
-
Name of the MCP client
final
- negotiatedProtocolVersion → String?
-
Negotiated protocol revision after initialize completes; null
before then.
no setter
-
onConnect
→ Stream<
ServerInfo> -
Stream of connection events
no setter
-
onDisconnect
→ Stream<
DisconnectReason> -
Stream of disconnection events
no setter
-
onError
→ Stream<
McpError> -
Stream of error events
no setter
- protocolVersion → String
-
Protocol version this client implements
final
-
roots
→ List<
Root> -
Locally configured roots (URI / filesystem boundaries the server is
allowed to operate in). Mutations push
notifications/roots/list_changedto the server.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- serverCapabilities → ServerCapabilities?
-
Get the server capabilities
no setter
-
serverInfo
→ Map<
String, dynamic> ? -
Get the server information
no setter
- version → String
-
Version of the MCP client implementation
final
Methods
-
addRoot(
Root root) → void -
Add a root to the client's local list. Server is notified via
notifications/roots/list_changed. -
addRootMap(
Map< String, dynamic> root) → void -
Map-based variant of addRoot for callers that don't have the
typed Root available. Constructs a Root from the supplied map
(must contain at least
uri) and delegates to addRoot. -
callTool(
String name, Map< String, dynamic> toolArguments) → Future<CallToolResult> - Call a tool on the server
-
callToolWithTracking(
String name, Map< String, dynamic> arguments, {bool trackProgress = true}) → Future<ToolCallTracking> - Call a tool and get the operation ID for tracking progress
-
connect(
ClientTransport transport) → Future< void> - Connect the client to a transport
-
connectWithRetry(
ClientTransport transport, {int maxRetries = 3, Duration delay = const Duration(seconds: 2)}) → Future< void> - Connect with retry mechanism
-
disconnect(
) → void - Disconnect the client from its transport
-
dispose(
) → void - Dispose client resources
-
getPrompt(
String name, [Map< String, dynamic> ? promptArguments]) → Future<GetPromptResult> - Get a prompt from the server
-
getResourceWithTemplate(
String templateUri, Map< String, dynamic> params) → Future<ReadResourceResult> - Get a resource using a template
-
initialize(
) → Future< void> - Initialize the connection to the server
-
listPrompts(
) → Future< List< Prompt> > - List available prompts on the server
-
listResources(
) → Future< List< Resource> > - List available resources on the server
-
listResourceTemplates(
) → Future< List< ResourceTemplate> > - List resource templates on the server
-
listTools(
) → Future< List< Tool> > - List available tools on the server
-
listToolsMetadata(
ToolRegistry registry) → Future< List< ToolMetadata> > -
Available on Client, provided by the ClientToolMetadataExtension extension
Fetch tools and return metadata only Caches full tools in provided registry for later schema lookup -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyCancelled(
String requestId, {String? reason}) → void - Cancel an in-flight server-side operation.
-
notifyProgress(
dynamic progressToken, double progress, {double? total, String? message}) → void -
Report progress on an in-flight server-initiated request (spec
notifications/progress).progressTokenis the token the server sent in the original request's_meta.progressToken. -
onElicitationRequest(
Future< Map< handler(Map<String, dynamic> >String, dynamic> params)) → void -
Register a handler for server-initiated
elicitation/createrequests (spec 2025-06-18). The handler shows the requested form to the user and returns{ action, content? }per spec —actionisaccept/decline/cancel. -
onListRoots(
Future< List< handler()) → voidRoot> > -
Register a custom handler for server-initiated
roots/listrequests. By default the client responds with the locally configured roots list — supply this to override. -
onListRootsMap(
Future< List< handler()) → voidMap< >String, dynamic> > -
Map-based variant of onListRoots. The handler returns the raw
list of root maps (each with at least a
urikey); this method wraps it in the spec response shape. Sibling to onSamplingRequestMap — same rationale. -
onLogging(
dynamic handler(McpLogLevel, String, String?, Map< String, dynamic> ?)) → void - Handle logging notification
-
onNotification(
String method, dynamic handler(Map< String, dynamic> )) → void - Register a notification handler
-
onProgress(
dynamic handler(String requestId, double progress, String message)) → void - Register a handler for progress updates from the server
-
onPromptsListChanged(
dynamic handler()) → void - Handle prompts list changed notification
-
onResourceContentUpdated(
dynamic handler(String uri, ResourceContentInfo content)) → void - Register a handler for resource update notifications with content
-
onResourcesListChanged(
dynamic handler()) → void - Handle resources list changed notification
-
onResourceUpdated(
dynamic handler(String)) → void - Handle resource updated notification
-
onRootsListChanged(
dynamic handler()) → void - Handle roots list changed notification
-
onSamplingRequest(
Future< CreateMessageResult> handler(CreateMessageRequest request)) → void -
Register a handler for server-initiated
sampling/createMessagerequests. The driver app (Claude Desktop / Claude Code / a vibe-style IDE) fulfils the prompt with its own LLM and returns the specCreateMessageResult(role,content,model, optionalstopReason). -
onSamplingRequestMap(
Future< Map< handler(Map<String, dynamic> >String, dynamic> params)) → void -
Map-based variant of onSamplingRequest. Receives the spec
CreateMessageRequest.paramsas a raw map and must return the rawCreateMessageResultmap. -
onToolsListChanged(
dynamic handler()) → void - Handle tools list changed notification
-
readResource(
String uri) → Future< ReadResourceResult> - Read a resource from the server
-
removeRoot(
String uri) → void -
Remove a root from the client's local list. Server is notified via
notifications/roots/list_changed. -
setLoggingLevel(
McpLogLevel level) → Future< void> - Set the logging level for the server
-
subscribeResource(
String uri) → Future< void> - Subscribe to a resource
-
toString(
) → String -
A string representation of this object.
inherited
-
unsubscribeResource(
String uri) → Future< void> - Unsubscribe from a resource
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited