|
| 1 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 2 | +import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'; |
| 3 | +import { BaseTool } from './index'; |
| 4 | + |
| 5 | +export interface MCPToolInfo { |
| 6 | + name: string; |
| 7 | + description?: string; |
| 8 | + inputSchema?: any; |
| 9 | +} |
| 10 | + |
| 11 | +export class MCPTool extends BaseTool { |
| 12 | + private client: Client; |
| 13 | + private inputSchema: any; |
| 14 | + |
| 15 | + constructor(info: MCPToolInfo, client: Client) { |
| 16 | + super(info.name, info.description || `Call the ${info.name} tool`); |
| 17 | + this.client = client; |
| 18 | + this.inputSchema = info.inputSchema || { type: 'object', properties: {}, required: [] }; |
| 19 | + } |
| 20 | + |
| 21 | + get schemaProperties(): Record<string, any> | undefined { |
| 22 | + return this.inputSchema?.properties; |
| 23 | + } |
| 24 | + |
| 25 | + async execute(args: any = {}): Promise<any> { |
| 26 | + try { |
| 27 | + const result: any = await this.client.callTool({ name: this.name, arguments: args }); |
| 28 | + if (result.structuredContent) { |
| 29 | + return result.structuredContent; |
| 30 | + } |
| 31 | + if (Array.isArray(result.content) && result.content.length > 0) { |
| 32 | + const item = result.content[0]; |
| 33 | + if (typeof item.text === 'string') return item.text; |
| 34 | + } |
| 35 | + return result; |
| 36 | + } catch (error) { |
| 37 | + throw new Error(`Failed to execute tool ${this.name}: ${error instanceof Error ? error.message : String(error)}`); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + toOpenAITool() { |
| 42 | + return { |
| 43 | + type: 'function', |
| 44 | + function: { |
| 45 | + name: this.name, |
| 46 | + description: this.description, |
| 47 | + parameters: this.inputSchema |
| 48 | + } |
| 49 | + }; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export class MCP implements Iterable<MCPTool> { |
| 54 | + tools: MCPTool[] = []; |
| 55 | + private client: Client | null = null; |
| 56 | + |
| 57 | + constructor(private url: string, private debug = false) { |
| 58 | + if (debug) { |
| 59 | + console.log(`MCP client initialized for URL: ${url}`); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + async initialize(): Promise<void> { |
| 64 | + if (this.client) { |
| 65 | + if (this.debug) console.log('MCP client already initialized'); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + this.client = new Client({ name: 'praisonai-ts-mcp', version: '1.0.0' }); |
| 71 | + const transport = new SSEClientTransport(new URL(this.url)); |
| 72 | + await this.client.connect(transport); |
| 73 | + const { tools } = await this.client.listTools(); |
| 74 | + this.tools = tools.map((t: any) => new MCPTool({ |
| 75 | + name: t.name, |
| 76 | + description: t.description, |
| 77 | + inputSchema: t.inputSchema |
| 78 | + }, this.client as Client)); |
| 79 | + |
| 80 | + if (this.debug) console.log(`Initialized MCP with ${this.tools.length} tools`); |
| 81 | + } catch (error) { |
| 82 | + if (this.client) { |
| 83 | + await this.client.close().catch(() => {}); |
| 84 | + this.client = null; |
| 85 | + } |
| 86 | + throw new Error(`Failed to initialize MCP client: ${error instanceof Error ? error.message : 'Unknown error'}`); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + [Symbol.iterator](): Iterator<MCPTool> { |
| 91 | + return this.tools[Symbol.iterator](); |
| 92 | + } |
| 93 | + |
| 94 | + toOpenAITools() { |
| 95 | + return this.tools.map(t => t.toOpenAITool()); |
| 96 | + } |
| 97 | + |
| 98 | + async close(): Promise<void> { |
| 99 | + if (this.client) { |
| 100 | + try { |
| 101 | + await this.client.close(); |
| 102 | + } catch (error) { |
| 103 | + if (this.debug) { |
| 104 | + console.warn('Error closing MCP client:', error); |
| 105 | + } |
| 106 | + } finally { |
| 107 | + this.client = null; |
| 108 | + this.tools = []; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + get isConnected(): boolean { |
| 114 | + return this.client !== null; |
| 115 | + } |
| 116 | +} |
0 commit comments