dart_node_mcp library
MCP (Model Context Protocol) server bindings for Dart on Node.js.
This package provides typed Dart bindings for the @modelcontextprotocol/sdk
npm package, enabling you to build MCP servers in Dart that run on Node.js.
Quick Start
import 'package:dart_node_mcp/dart_node_mcp.dart';
import 'package:nadz/nadz.dart';
Future<void> main() async {
// Create server
final serverResult = McpServer.create(
(name: 'my-server', version: '1.0.0'),
);
final server = switch (serverResult) {
Success(:final value) => value,
Error(:final error) => throw Exception(error),
};
// Register a tool
server.registerTool(
'echo',
(description: 'Echo input back', inputSchema: null, ...),
(args, meta) async => (
content: [(type: 'text', text: args['message'] as String)],
isError: false,
),
);
// Create transport and connect
final transportResult = createStdioServerTransport();
final transport = switch (transportResult) {
Success(:final value) => value,
Error(:final error) => throw Exception(error),
};
await server.connect(transport);
}
High-level MCP Server (wraps TypeScript McpServer class).
Low-level MCP Server (wraps TypeScript Server class).
StdioServerTransport
Stdio server transport (matches TypeScript StdioServerTransport).
StreamableHttpTransport
Streamable HTTP server transport (matches TypeScript
StreamableHTTPServerTransport).
Transport
Transport interface (matches TypeScript Transport type).
Create low-level Server.
createStdioServerTransport()
→ Result<StdioServerTransport, String>
Create stdio transport with default stdin/stdout.
createStdioServerTransportWithStreams(JSObject stdin, JSObject stdout)
→ Result<StdioServerTransport, String>
Create stdio transport with custom stdin/stdout streams.
createStreamableHttpTransport({String sessionIdGenerator()?, void onSessionInitialized(String sessionId)?})
→ Result<StreamableHttpTransport, String>
Create a stateful Streamable HTTP transport.
CallToolResult
= ({List<Object> content, bool? isError})
Tool call result.
GetPromptResult
= ({String? description, List<PromptMessage> messages})
Get prompt result.
ImageContent
= ({String data, String mimeType, String type})
Image content in tool results (base64 encoded).
Implementation
= ({String name, String version})
Server implementation info (matches TypeScript Implementation type).
JsonRpcMessage
= ({Object? error, Object? id, String jsonrpc, String? method, Object? params, Object? result})
JSON-RPC message for transport.
LoggingCapability
= ({bool? enabled})
Logging capability configuration.
LoggingMessageParams
= ({Object? data, String level, String? logger})
Logging message parameters.
PromptCallback
= Future<GetPromptResult> Function(Map<String, String> args)
Prompt callback function type.
PromptConfig
= ({Map<String, Object?>? argsSchema, String? description, String? title})
Prompt configuration for registration.
PromptMessage
= ({Object content, String role})
Prompt message in prompt results.
PromptsCapability
= ({bool? listChanged})
Prompts capability configuration.
ReadResourceCallback
= Future<ReadResourceResult> Function(String uri)
Read resource callback function type.
ReadResourceResult
= ({List<Object> contents})
Read resource result.
ReadResourceTemplateCallback
= Future<ReadResourceResult> Function(String uri, Map<String, String> variables)
Read resource template callback function type.
RegisteredPrompt
= ({String name, void Function() remove, void Function(PromptConfig config) update})
Registered prompt returned from registerPrompt.
RegisteredResource
= ({String name, void Function() remove, void Function(ResourceMetadata metadata) update, String uri})
Registered resource returned from registerResource.
RegisteredResourceTemplate
= ({String name, void Function() remove, void Function(ResourceMetadata metadata) update, String uriTemplate})
Registered resource template returned from registerResourceTemplate.
RegisteredTool
= ({void Function() disable, void Function() enable, String name, void Function() remove, void Function(ToolConfig config) update})
Registered tool returned from registerTool.
ResourceContent
= ({String? mimeType, String? text, String type, String uri})
Resource content in tool results.
ResourceMetadata
= ({String? description, String? mimeType})
Resource metadata.
ResourcesCapability
= ({bool? listChanged, bool? subscribe})
Resources capability configuration.
ResourceTemplate
= ({String? description, String? mimeType, String? name, String uriTemplate})
Resource template for URI patterns.
ResourceUpdatedParams
= ({String uri})
Resource updated notification params.
ServerCapabilities
= ({LoggingCapability? logging, PromptsCapability? prompts, ResourcesCapability? resources, ToolsCapability? tools})
Server capabilities.
ServerOptions
= ({ServerCapabilities? capabilities, String? instructions})
Server options for initialization.
StreamableHttpTransportOptions
= ({JSFunction? onsessioninitialized, JSFunction? sessionIdGenerator})
Options for creating a StreamableHttpTransport.
TextContent
= ({String text, String type})
Text content in tool results.
ToolAnnotations
= ({bool? destructiveHint, bool? idempotentHint, bool? openWorldHint, bool? readOnlyHint, String? title})
Tool annotations providing hints about tool behavior.
ToolCallback
= Future<CallToolResult> Function(Map<String, Object?> args, ToolCallMeta? meta)
Tool callback function type (matches ToolCallback<Args>).
ToolCallMeta
= ({String? progressToken})
Tool call metadata.
ToolConfig
= ({ToolAnnotations? annotations, String? description, Map<String, Object?>? inputSchema, Map<String, Object?>? outputSchema, String? title})
Tool configuration for registration (matches registerTool config param).
ToolsCapability
= ({bool? listChanged})
Tools capability configuration.
TransportCloseCallback
= void Function()
Event callback type for transport close.
TransportErrorCallback
= void Function(JSAny error)
Event callback type for transport errors.
TransportMessageCallback
= void Function(JSObject message)
Event callback type for transport messages.