diff options
| author | Harold Mciver <[email protected]> | 2025-08-04 17:38:23 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-04 21:38:23 +0000 |
| commit | 99ba2f6424b8873df3857f03b729e236710bbc32 (patch) | |
| tree | 73f6b72d6fdc7d546114705b4deb133aadbd4cdb /packages/core/src | |
| parent | 93f8fe3671babbd3065d7a80b9e5ac50c42042da (diff) | |
Update MCP client to connect to servers with only prompts (#5290)
Diffstat (limited to 'packages/core/src')
| -rw-r--r-- | packages/core/src/tools/mcp-client.ts | 71 |
1 files changed, 49 insertions, 22 deletions
diff --git a/packages/core/src/tools/mcp-client.ts b/packages/core/src/tools/mcp-client.ts index f9ccc380..00f2197a 100644 --- a/packages/core/src/tools/mcp-client.ts +++ b/packages/core/src/tools/mcp-client.ts @@ -366,33 +366,47 @@ export async function connectAndDiscover( ): Promise<void> { updateMCPServerStatus(mcpServerName, MCPServerStatus.CONNECTING); + let mcpClient: Client | undefined; try { - const mcpClient = await connectToMcpServer( + mcpClient = await connectToMcpServer( mcpServerName, mcpServerConfig, debugMode, ); - try { - updateMCPServerStatus(mcpServerName, MCPServerStatus.CONNECTED); - mcpClient.onerror = (error) => { - console.error(`MCP ERROR (${mcpServerName}):`, error.toString()); - updateMCPServerStatus(mcpServerName, MCPServerStatus.DISCONNECTED); - }; - await discoverPrompts(mcpServerName, mcpClient, promptRegistry); - const tools = await discoverTools( - mcpServerName, - mcpServerConfig, - mcpClient, - ); - for (const tool of tools) { - toolRegistry.registerTool(tool); - } - } catch (error) { - mcpClient.close(); - throw error; + mcpClient.onerror = (error) => { + console.error(`MCP ERROR (${mcpServerName}):`, error.toString()); + updateMCPServerStatus(mcpServerName, MCPServerStatus.DISCONNECTED); + }; + + // Attempt to discover both prompts and tools + const prompts = await discoverPrompts( + mcpServerName, + mcpClient, + promptRegistry, + ); + const tools = await discoverTools( + mcpServerName, + mcpServerConfig, + mcpClient, + ); + + // If we have neither prompts nor tools, it's a failed discovery + if (prompts.length === 0 && tools.length === 0) { + throw new Error('No prompts or tools found on the server.'); + } + + // If we found anything, the server is connected + updateMCPServerStatus(mcpServerName, MCPServerStatus.CONNECTED); + + // Register any discovered tools + for (const tool of tools) { + toolRegistry.registerTool(tool); } } catch (error) { + if (mcpClient) { + mcpClient.close(); + } console.error( `Error connecting to MCP server '${mcpServerName}': ${getErrorMessage( error, @@ -423,7 +437,8 @@ export async function discoverTools( const tool = await mcpCallableTool.tool(); if (!Array.isArray(tool.functionDeclarations)) { - throw new Error(`Server did not return valid function declarations.`); + // This is a valid case for a prompt-only server + return []; } const discoveredTools: DiscoveredMCPTool[] = []; @@ -454,7 +469,17 @@ export async function discoverTools( } return discoveredTools; } catch (error) { - throw new Error(`Error discovering tools: ${error}`); + if ( + error instanceof Error && + !error.message?.includes('Method not found') + ) { + console.error( + `Error discovering tools from ${mcpServerName}: ${getErrorMessage( + error, + )}`, + ); + } + return []; } } @@ -469,7 +494,7 @@ export async function discoverPrompts( mcpServerName: string, mcpClient: Client, promptRegistry: PromptRegistry, -): Promise<void> { +): Promise<Prompt[]> { try { const response = await mcpClient.request( { method: 'prompts/list', params: {} }, @@ -484,6 +509,7 @@ export async function discoverPrompts( invokeMcpPrompt(mcpServerName, mcpClient, prompt.name, params), }); } + return response.prompts; } catch (error) { // It's okay if this fails, not all servers will have prompts. // Don't log an error if the method is not found, which is a common case. @@ -497,6 +523,7 @@ export async function discoverPrompts( )}`, ); } + return []; } } |
