-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): add Model Context Protocol tool
Signed-off-by: Tomas Pilar <[email protected]>
- Loading branch information
1 parent
fb2153c
commit 76c500b
Showing
7 changed files
with
383 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2025 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
import { MCPTool } from "bee-agent-framework/tools/mcp"; | ||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; | ||
import { BeeAgent } from "bee-agent-framework/agents/bee/agent"; | ||
import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMemory"; | ||
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat"; | ||
|
||
// Create MCP Client | ||
const client = new Client( | ||
{ | ||
name: "test-client", | ||
version: "1.0.0", | ||
}, | ||
{ | ||
capabilities: {}, | ||
}, | ||
); | ||
|
||
// Connect the client to any MCP server with tools capablity | ||
await client.connect( | ||
new StdioClientTransport({ | ||
command: "npx", | ||
args: ["-y", "@modelcontextprotocol/server-everything"], | ||
}), | ||
); | ||
|
||
try { | ||
// Server usually supports several tools, use the factory for automatic discovery | ||
const tools = await MCPTool.createTools(client); | ||
const agent = new BeeAgent({ | ||
llm: new OllamaChatLLM(), | ||
memory: new UnconstrainedMemory(), | ||
tools, | ||
}); | ||
// @modelcontextprotocol/server-everything contains "add" tool | ||
await agent.run({ prompt: "Find out how much is 4 + 7" }).observe((emitter) => { | ||
emitter.on("update", async ({ data, update, meta }) => { | ||
console.log(`Agent (${update.key}) 🤖 : `, update.value); | ||
}); | ||
}); | ||
} finally { | ||
// Close the MCP connection | ||
await client.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright 2025 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Server } from "@modelcontextprotocol/sdk/server/index.js"; | ||
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; | ||
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; | ||
import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
import { MCPTool } from "./mcp.js"; | ||
import { entries } from "remeda"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
import { z } from "zod"; | ||
|
||
const abInputSchema = z.object({ a: z.number(), b: z.number() }); | ||
const toolDescriptions = { | ||
add: { | ||
description: "Adds two numbers", | ||
inputSchema: zodToJsonSchema(abInputSchema), | ||
handler: ({ a, b }: z.input<typeof abInputSchema>) => a + b, | ||
}, | ||
multiply: { | ||
description: "Multiplies two numbers", | ||
inputSchema: zodToJsonSchema(abInputSchema), | ||
handler: ({ a, b }: z.input<typeof abInputSchema>) => a * b, | ||
}, | ||
} as const; | ||
|
||
describe("MCPTool", () => { | ||
let server: Server; | ||
let client: Client; | ||
let tools: MCPTool[]; | ||
|
||
beforeEach(async () => { | ||
server = new Server( | ||
{ | ||
name: "test-server", | ||
version: "1.0.0", | ||
}, | ||
{ | ||
capabilities: { | ||
tools: {}, | ||
}, | ||
}, | ||
); | ||
server.setRequestHandler(ListToolsRequestSchema, async () => { | ||
return { | ||
tools: entries(toolDescriptions).map(([name, { description, inputSchema }]) => ({ | ||
name, | ||
description, | ||
inputSchema, | ||
})), | ||
}; | ||
}); | ||
server.setRequestHandler(CallToolRequestSchema, async (request) => { | ||
const tool = toolDescriptions[request.params.name as keyof typeof toolDescriptions]; | ||
if (!tool) { | ||
throw new Error("Tool not found"); | ||
} | ||
// Arguments are assumed to be valid in this mock | ||
return { | ||
contents: [tool.handler(request.params.arguments as any)], | ||
}; | ||
}); | ||
|
||
client = new Client( | ||
{ | ||
name: "test-client", | ||
version: "1.0.0", | ||
}, | ||
{ | ||
capabilities: {}, | ||
}, | ||
); | ||
|
||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
await server.connect(serverTransport); | ||
await client.connect(clientTransport); | ||
|
||
tools = await MCPTool.createTools(client); | ||
}); | ||
|
||
it("should run the tools", async () => { | ||
const tool = tools.at(0); | ||
expect(tool).toBeDefined(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await client.close(); | ||
await server.close(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright 2025 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BaseToolRunOptions, ToolEmitter, ToolInput, JSONToolOutput, Tool } from "@/tools/base.js"; | ||
import { Emitter } from "@/emitter/emitter.js"; | ||
import { GetRunContext } from "@/context.js"; | ||
import { Client as MCPClient } from "@modelcontextprotocol/sdk/client/index.js"; | ||
import { ListToolsResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { SchemaObject } from "ajv"; | ||
|
||
export interface MCPToolInput { | ||
client: MCPClient; | ||
tool: ListToolsResult["tools"][number]; | ||
} | ||
|
||
export class MCPToolOutput extends JSONToolOutput<any> {} | ||
|
||
export class MCPTool extends Tool<MCPToolOutput> { | ||
public readonly name: string; | ||
public readonly description: string; | ||
|
||
public readonly client: MCPClient; | ||
private readonly tool: ListToolsResult["tools"][number]; | ||
|
||
constructor({ client, tool, ...options }: MCPToolInput) { | ||
super(options); | ||
this.client = client; | ||
this.tool = tool; | ||
this.name = tool.name; | ||
this.description = tool.description ?? "No description, use based on name."; | ||
} | ||
|
||
public readonly emitter: ToolEmitter<ToolInput<this>, MCPToolOutput> = Emitter.root.child({ | ||
namespace: ["tool", "mcp", "tool"], | ||
creator: this, | ||
}); | ||
|
||
inputSchema() { | ||
return this.tool.inputSchema as SchemaObject; | ||
} | ||
|
||
protected async _run( | ||
input: ToolInput<this>, | ||
_options: BaseToolRunOptions, | ||
run: GetRunContext<typeof this>, | ||
) { | ||
const result = await this.client.callTool({ name: this.name, arguments: input }, undefined, { | ||
signal: run.signal, | ||
}); | ||
return new MCPToolOutput(result); | ||
} | ||
|
||
static async createTools(client: MCPClient): Promise<MCPTool[]> { | ||
const { tools } = await client.listTools(); | ||
return tools.map((tool) => new MCPTool({ client, tool })); | ||
} | ||
} |
Oops, something went wrong.