xonecas.io
5875
Os

ollama-space-molt-player / files

ollama-space-molt-player / packages / client / src / mcp-client.ts

3.2 KB
1import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
3import type { Tool as MCPTool } from "@modelcontextprotocol/sdk/types.js";
4import { TOOL_SCHEMA_PATCHES } from "./tool-schemas.ts";
5
6export interface OllamaTool {
7	type: "function";
8	function: {
9		name: string;
10		description: string;
11		parameters: {
12			type: string;
13			properties: Record<string, unknown>;
14			required: string[];
15		};
16	};
17}
18
19export interface ToolCallResult {
20	success: boolean;
21	content: unknown;
22	error?: string;
23}
24
25export class MCPClient {
26	private client: Client;
27	private transport: StreamableHTTPClientTransport | null = null;
28	private tools: MCPTool[] = [];
29	private serverUrl: string;
30
31	constructor(serverUrl: string = "https://game.spacemolt.com/mcp") {
32		this.serverUrl = serverUrl;
33		this.client = new Client({
34			name: "spacemolt-ollama-player",
35			version: "1.0.0",
36		});
37	}
38
39	async connect(): Promise<void> {
40		const url = new URL(this.serverUrl);
41		this.transport = new StreamableHTTPClientTransport(url);
42		await this.client.connect(this.transport);
43
44		const toolsResponse = await this.client.listTools();
45		this.tools = toolsResponse.tools;
46	}
47
48	async disconnect(): Promise<void> {
49		if (this.transport) {
50			await this.client.close();
51			this.transport = null;
52		}
53	}
54
55	getMCPTools(): MCPTool[] {
56		return this.tools;
57	}
58
59	getOllamaTools(): OllamaTool[] {
60		return this.tools.map((tool) => this.convertToOllamaTool(tool));
61	}
62
63	private convertToOllamaTool(mcpTool: MCPTool): OllamaTool {
64		const inputSchema = mcpTool.inputSchema || { type: "object", properties: {}, required: [] };
65
66		let properties = (inputSchema.properties as Record<string, unknown>) || {};
67		let required = (inputSchema.required as string[]) || [];
68
69		const patch = TOOL_SCHEMA_PATCHES[mcpTool.name];
70		if (patch && Object.keys(properties).length === 0) {
71			properties = patch.properties;
72			required = patch.required;
73		}
74
75		return {
76			type: "function",
77			function: {
78				name: mcpTool.name,
79				description: mcpTool.description || "",
80				parameters: {
81					type: inputSchema.type as string,
82					properties,
83					required,
84				},
85			},
86		};
87	}
88
89	async callTool(name: string, args: Record<string, unknown>): Promise<ToolCallResult> {
90		try {
91			const result = await this.client.callTool({ name, arguments: args });
92
93			const content = result.content;
94			let parsedContent: unknown = content;
95
96			if (Array.isArray(content) && content.length > 0) {
97				const firstItem = content[0];
98				if (firstItem && typeof firstItem === "object" && "text" in firstItem) {
99					const textContent = (firstItem as { text: string }).text;
100					try {
101						parsedContent = JSON.parse(textContent);
102					} catch {
103						parsedContent = textContent;
104					}
105				}
106			}
107
108			return {
109				success: !result.isError,
110				content: parsedContent,
111				error: result.isError ? String(parsedContent) : undefined,
112			};
113		} catch (error) {
114			return {
115				success: false,
116				content: null,
117				error: error instanceof Error ? error.message : String(error),
118			};
119		}
120	}
121
122	isConnected(): boolean {
123		return this.transport !== null;
124	}
125
126	getToolCount(): number {
127		return this.tools.length;
128	}
129}