mcpAgentTool function

AgentTool mcpAgentTool({
  1. required String server,
  2. required McpToolInfo tool,
  3. required McpToolCaller caller,
})

Builds the AgentTool for one advertised MCP tool.

Implementation

AgentTool mcpAgentTool({
  required String server,
  required McpToolInfo tool,
  required McpToolCaller caller,
}) {
  final description = StringBuffer("MCP tool from server '$server'.");
  final toolDescription = tool.description;
  if (toolDescription != null && toolDescription.isNotEmpty) {
    description.write(' $toolDescription');
  }
  return AgentTool(
    name: mcpToolName(server, tool.name),
    label: 'mcp:$server/${tool.name}',
    // MCP tools run arbitrary server-side actions: the exec tier keeps the
    // approval gate's safest default for them.
    tier: ApprovalTier.exec,
    description: description.toString(),
    parameters:
        tool.inputSchema ??
        const {'type': 'object', 'properties': <String, dynamic>{}},
    execute: (arguments, cancelToken, onUpdate) async {
      cancelToken?.throwIfCancelled();
      final result = await caller(server, tool.name, arguments);
      cancelToken?.throwIfCancelled();
      return mcpResultToToolResult(result);
    },
  );
}