Skip to content

Commit bce131f

Browse files
committed
feat: add disableToolIdMangling option to McpServerConfig and update related implementations
1 parent eb14a70 commit bce131f

5 files changed

Lines changed: 54 additions & 27 deletions

File tree

docs/src/content/docs/reference/scripts/mcp-clients.mdx

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ hero:
1111
details include a small play button or gear symbol, symbolizing operations
1212
or scripting functionality.
1313
file: ./mcp-clients.png
14-
1514
---
1615

1716
The [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) defines a protocol for sharing [tools](https://modelcontextprotocol.io/docs/concepts/tools)
@@ -40,10 +39,10 @@ This identifier is used to reference the server in the `mcpClient`.
4039

4140
```js
4241
const fs = await host.mcpServer({
43-
id: "filesystem",
44-
command: "npx",
45-
args: ["-y", "@modelcontextprotocol/server-filesystem", path.resolve(".")],
46-
})
42+
id: "filesystem",
43+
command: "npx",
44+
args: ["-y", "@modelcontextprotocol/server-filesystem", path.resolve(".")],
45+
});
4746
```
4847

4948
The server is automatically stopped when the prompt finishes.
@@ -52,28 +51,28 @@ The server is automatically stopped when the prompt finishes.
5251

5352
You can perform operations on tools. Queries are not cached and always communicate with the server.
5453

55-
- List tools:
54+
- List tools (metadata):
5655

57-
```js
58-
const tools = await fs.listTools()
59-
```
56+
```js
57+
const tools = await fs.listTools();
58+
```
6059

6160
- Call a tool:
6261

63-
```js
64-
const res = await fs.callTool("get_file_info", { path: "README.md" })
65-
```
62+
```js
63+
const res = await fs.callTool("get_file_info", { path: "README.md" });
64+
```
6665

6766
- Use the result:
6867

69-
```js
70-
const info = res.content[0].text
71-
```
68+
```js
69+
const info = res.content[0].text;
70+
```
7271

7372
The structure of the output depends on the tool, but it is designed to be consumed by an LLM. You will likely want to use `def` to store it in your prompt:
7473

7574
```js
76-
def("INFO", info)
75+
def("INFO", info);
7776
```
7877

7978
## Example: YouTube Transcript
@@ -83,12 +82,28 @@ of a YouTube video. It is listed in the [Docker MCP Catalog](https://hub.docker.
8382

8483
```js
8584
const yt = await host.mcpServer({
86-
id: "youtube_transcript",
87-
command: "docker",
88-
args: ["run", "-i", "--rm", "mcp/youtube-transcript"],
89-
})
90-
91-
const url = "https://youtu.be/ENunZe--7j0"
92-
const transcript = await yt.callTool("get_transcript", { url })
93-
console.log(`transcript: ${transcript.text}`)
85+
id: "youtube_transcript",
86+
command: "docker",
87+
args: ["run", "-i", "--rm", "mcp/youtube-transcript"],
88+
});
89+
90+
const url = "https://youtu.be/ENunZe--7j0";
91+
const transcript = await yt.callTool("get_transcript", { url });
92+
console.log(`transcript: ${transcript.text}`);
93+
```
94+
95+
## Sharing MCPs
96+
97+
By default, MCP servers are not shared between prompts. If you want to use the same MCP server in multiple prompts,
98+
you can use the `host.mcpClient` function to create a client that can be reused across prompts.
99+
100+
You can also get tools in a form that can be used in `defTool` and reuse it in multiple prompts:
101+
102+
```js
103+
const tools = await fs.listToolCallbacks();
104+
105+
await runPrompt((ctx) => {
106+
for (const tool of tools) ctx.defTool(tools);
107+
ctx....
108+
});
94109
```

packages/core/src/mcpclient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class McpClientManager extends EventTarget implements AsyncDisposable {
8383
tools: _toolsConfig,
8484
generator,
8585
intent,
86+
disableToolIdMangling,
8687
env: unresolvedEnv,
8788
...rest
8889
} = serverConfig;
@@ -203,7 +204,7 @@ export class McpClientManager extends EventTarget implements AsyncDisposable {
203204
} satisfies DefToolOptions;
204205
return {
205206
spec: {
206-
name: `${id}_${name}`,
207+
name: disableToolIdMangling ? name : `${id}_${name}`,
207208
description,
208209
// eslint-disable-next-line @typescript-eslint/no-explicit-any
209210
parameters: inputSchema as any,

packages/core/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,6 +4703,11 @@ export interface McpServerConfig extends ContentSafetyOptions {
47034703
*/
47044704
cwd?: string;
47054705

4706+
/**
4707+
* Do not prepend client identifier with the tool id.
4708+
*/
4709+
disableToolIdMangling?: boolean;
4710+
47064711
id: string;
47074712
options?: DefToolOptions;
47084713

packages/core/src/types/prompt_template.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,6 +4693,11 @@ interface McpServerConfig extends ContentSafetyOptions {
46934693
id: string;
46944694
options?: DefToolOptions;
46954695

4696+
/**
4697+
* Do not prepend client identifier with the tool id.
4698+
*/
4699+
disableToolIdMangling?: boolean;
4700+
46964701
/**
46974702
* A list of allowed tools and their specifications. This filtering is applied
46984703
* before computing the sha signature.

packages/sample/genaisrc/mcp-reuse.genai.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ const { output } = env;
33
const fetchClient = await host.mcpServer({
44
id: "fetch",
55
command: "docker",
6-
args: ["run", "-i", "--rm", "mcp/fetch"],
6+
args: ["run", "-i", "--rm", "mcp/fetch"],
7+
disableToolIdMangling: true
78
});
89
const fetchTools = await fetchClient.listToolCallbacks();
910
output.itemValue(`tools`, fetchTools.map((t) => t.spec.name).join(", "));
1011
const fetchTool = await (
1112
await fetchClient.listToolCallbacks()
12-
).find((t) => t.spec.name === "fetch_fetch");
13+
).find((t) => t.spec.name === "fetch");
1314
if (!fetchTool) cancel("fetch tool not found");
1415
const urls = [
1516
"https://raw.githubusercontent.com/microsoft/genaiscript/refs/heads/main/SUPPORT.md",

0 commit comments

Comments
 (0)