This commit is contained in:
Ashwin Bhat
2024-10-08 07:33:47 -07:00
parent 51cde09845
commit 38e1c8b2b9
5 changed files with 189 additions and 10 deletions

View File

@@ -10,6 +10,10 @@ import {
ListPromptsResultSchema,
GetPromptResult,
GetPromptResultSchema,
ListToolsResult,
ListToolsResultSchema,
CallToolResult,
CallToolResultSchema,
} from "mcp-typescript/types.js";
export class McpClient {
@@ -86,6 +90,29 @@ export class McpClient {
);
}
// Tool Operations
async listTools(): Promise<ListToolsResult> {
return await this.client.request(
{
method: "tools/list",
},
ListToolsResultSchema,
);
}
async callTool(
name: string,
params: Record<string, unknown>,
): Promise<CallToolResult> {
return await this.client.request(
{
method: "tools/call",
params: { name, ...params },
},
CallToolResultSchema,
);
}
getServerCapabilities() {
return this.client.getServerCapabilities();
}

View File

@@ -30,6 +30,19 @@ wss.on("connection", (ws: WebSocket) => {
} else if (command.type === "getPrompt" && command.name) {
const prompt = await mcpClient.getPrompt(command.name);
ws.send(JSON.stringify({ type: "prompt", data: prompt }));
} else if (command.type === "listTools") {
const tools = await mcpClient.listTools();
ws.send(JSON.stringify({ type: "tools", data: tools }));
} else if (
command.type === "callTool" &&
command.name &&
command.params
) {
const result = await mcpClient.callTool(
command.name + "asdf",
command.params,
);
ws.send(JSON.stringify({ type: "toolResult", data: result }));
}
} catch (error) {
console.error("Error:", error);