-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.ts
109 lines (90 loc) · 2.63 KB
/
http_server.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import OpenAI from 'openai';
const oaiClient = new OpenAI({
apiKey: process.env["OPENROUTER_API_KEY"],
baseURL: "https://openrouter.ai/api/v1",
});
const transport = new StdioClientTransport({
command: "bun",
args: ["mcp_server.ts"]
});
const mcpClient = new Client(
{
name: "pokemon-client",
version: "1.0.0"
},
{
capabilities: {
prompts: {},
resources: {},
tools: {}
}
}
);
await mcpClient.connect(transport);
async function randomMcpAttack() {
// get our available tools
const tools = await mcpClient.listTools();
const chatCompletion = await oaiClient.chat.completions.create({
messages: [
{
role: 'system',
content: "You are a pokemon trainer. You have various tools at your disposal to perform your duties."
},
{
role: 'user',
content: 'Select a pokemon and a move and execute an attack'
}
],
model: 'openai/gpt-4o-mini',
tools: mapToolSchema(tools) // pass these tools into our model
});
const message = chatCompletion.choices[0].message
if (!message.tool_calls || message.tool_calls.length === 0) {
return null
}
return message.tool_calls[0].function
}
function mapToolSchema(inputSchema: any) {
if (!inputSchema.tools || !Array.isArray(inputSchema.tools)) {
throw new Error('Input must contain a tools array');
}
return inputSchema.tools.map((tool: any) => {
// Get the input schema from the tool
const schema = tool.inputSchema;
return {
"type": "function",
"function": {
"name": tool.name,
"description": tool.description || `Execute the ${tool.name} function`,
"parameters": {
...schema,
"$schema": undefined // Remove the $schema property as it's not needed
},
"strict": true
}
};
});
}
const server = Bun.serve({
port: 3005,
async fetch(request) {
const url = new URL(request.url);
console.log(url.pathname)
if (url.pathname === "/tools") {
const tools = await mcpClient.listTools();
return new Response(JSON.stringify(tools));
}
if (url.pathname === "/attack") {
const attackRes = await randomMcpAttack()
const attackResult = await mcpClient.callTool({
name: attackRes!.name,
arguments: JSON.parse(attackRes!.arguments)
});
return new Response(JSON.stringify(attackResult));
}
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on ${server.url}`);