MCP (Model Context Protocol) Support

Extend agents with external tools from MCP servers - remote APIs or local processes.

Remote MCP Servers

// Connect to remote MCP server
final huggingFace = McpClient.remote(
  'huggingface',
  url: Uri.parse('https://hf.co/mcp'),
);

// Get tools from the server
final tools = await huggingFace.listTools();

// Use with your agent
final agent = Agent('google', tools: tools);
final result = await agent.send('Who is hugging face?');

Local MCP Servers

Connect to local processes:

final calculator = McpClient.local(
  'calculator',
  command: 'npx',
  args: ['-y', '@modelcontextprotocol/server-calculator'],
);

final tools = await calculator.listTools();
final agent = Agent('anthropic', tools: [...tools]);

await agent.send('What is 15 multiplied by 27?');

Combining Multiple Sources

Mix local tools, MCP servers, and custom tools:

// Local tool
final timeTool = Tool(
  name: 'local_time',
  description: 'Get current time',
  onCall: (_) async => {'time': DateTime.now().toIso8601String()},
);

// MCP servers
final wiki = McpClient.remote('wiki', url: wikiUrl);
final hf = McpClient.remote('hf', url: hfUrl);

// Combine everything
final agent = Agent('google', tools: [
  timeTool,
  ...await wiki.listTools(),
  ...await hf.listTools(),
]);

await agent.send('What time is it and who is hugging face?');

Examples

Next Steps