-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecbox-mcp-server.ts
More file actions
86 lines (76 loc) · 2.17 KB
/
execbox-mcp-server.ts
File metadata and controls
86 lines (76 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod";
import { codeMcpServer } from "@execbox/core/mcp";
import { QuickJsExecutor } from "@execbox/quickjs";
function createUpstreamServer(): McpServer {
const server = new McpServer({
name: "upstream",
version: "1.0.0",
});
server.registerTool(
"search-docs",
{
description: "Search documentation.",
inputSchema: {
query: z.string(),
},
outputSchema: {
hits: z.array(z.string()),
},
},
async (args) => ({
content: [{ text: `found ${args.query}`, type: "text" }],
structuredContent: {
hits: [args.query],
},
}),
);
return server;
}
async function connectClient(server: McpServer): Promise<Client> {
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
const client = new Client({
name: "example-client",
version: "1.0.0",
});
await Promise.all([
server.connect(serverTransport),
client.connect(clientTransport),
]);
return client;
}
async function main(): Promise<void> {
const upstreamClient = await connectClient(createUpstreamServer());
const wrappedServer = await codeMcpServer(
{ client: upstreamClient },
{ executor: new QuickJsExecutor() },
);
const wrappedClient = await connectClient(wrappedServer);
const tools = await wrappedClient.listTools();
const searchResult = await wrappedClient.callTool({
name: "mcp_search_tools",
arguments: { query: "search" },
});
const executeResult = await wrappedClient.callTool({
name: "mcp_execute_code",
arguments: {
code: '(await mcp.search_docs({ query: "quickjs" })).structuredContent.hits[0]',
},
});
console.log("mcp server example result");
console.log(
JSON.stringify(
{
executeResult: executeResult.structuredContent,
searchResult: searchResult.structuredContent,
toolNames: tools.tools.map((tool) => tool.name),
},
null,
2,
),
);
}
void main();