Skip to content

Commit

Permalink
docs(tools): additional documentation for MCPTool usage (#273)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <[email protected]>
  • Loading branch information
pilartomas authored Jan 10, 2025
1 parent 9295593 commit c7f88a2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
55 changes: 55 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,61 @@ _Source: [examples/tools/custom/python.ts](/examples/tools/custom/python.ts)_
> Custom tools are executed within the code interpreter, but they cannot access any files.
> Only `PythonTool` does.
### Using the `MCPTool` class

The `MCPTool` allows you to instantiate tools given a connection to [MCP server](https://modelcontextprotocol.io/examples) with tools capability.

<!-- embedme examples/tools/mcp.ts -->

```ts
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.fromClient(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();
}
```

_Source: [examples/tools/mcp.ts](/examples/tools/mcp.ts)_

## General Tips

### Data Minimization
Expand Down
16 changes: 0 additions & 16 deletions examples/tools/mcp.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/**
* 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";
Expand Down

0 comments on commit c7f88a2

Please sign in to comment.