Skip to content

Commit

Permalink
Merge pull request #590 from sugarforever/fix/missing-mcp-servers-config
Browse files Browse the repository at this point in the history
fix: missing MCP servers config should not cause app failure
  • Loading branch information
sugarforever authored Dec 17, 2024
2 parents f95278d + 2801202 commit 1872cd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
7 changes: 5 additions & 2 deletions server/api/models/chat/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { McpService } from '@/server/utils/mcp'
import { zodToJsonSchema } from 'zod-to-json-schema'
import { ChatOllama } from '@langchain/ollama'
import { tool } from '@langchain/core/tools'
import { BaseChatModel } from '@langchain/core/language_models/chat_models'

interface RequestBody {
knowledgebaseId: number
Expand Down Expand Up @@ -185,7 +186,7 @@ export default defineEventHandler(async (event) => {
acc[tool.name] = tool
return acc
}, {})
if (family === MODEL_FAMILIES.anthropic) {
if (family === MODEL_FAMILIES.anthropic && normalizedTools?.length) {
/*
if (family === MODEL_FAMILIES.gemini) {
llm = llm.bindTools(normalizedTools.map((t) => {
Expand All @@ -200,7 +201,9 @@ export default defineEventHandler(async (event) => {
llm = llm.bindTools(normalizedTools)
}
*/
llm = llm.bindTools(normalizedTools)
if (llm?.bindTools) {
llm = llm.bindTools(normalizedTools) as BaseChatModel
}
} else if (llm instanceof ChatOllama) {
/*
console.log("Binding tools to ChatOllama")
Expand Down
43 changes: 28 additions & 15 deletions server/utils/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
import { DynamicStructuredTool } from "@langchain/core/tools"
import fs from 'fs'
import path from 'path'
import zodToJsonSchema from 'zod-to-json-schema'
interface McpServerConfig {
command: string
args: string[]
Expand All @@ -24,24 +23,38 @@ export class McpService {
}
console.log("Loading MCP servers from ", mcpConfigPath)

const mcpConfig = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf8'))
if (!fs.existsSync(mcpConfigPath)) {
return []
}

const allTools: any[] = []
for (const [serverName, serverConfig] of Object.entries(mcpConfig.mcpServers)) {
console.log("Server name: ", serverName)
console.log("Server config: ", serverConfig)
try {
const mcpConfig = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf8')) as McpConfig

const transport = new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args,
env: serverConfig.env
})
const allTools: any[] = []
for (const [serverName, serverConfig] of Object.entries(mcpConfig.mcpServers)) {
try {
console.log("Server name: ", serverName)
console.log("Server config: ", serverConfig)

const serverTools = await this.getToolsFromTransport(transport)
allTools.push(...serverTools)
}
const transport = new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args,
env: serverConfig.env
})

return allTools
const serverTools = await this.getToolsFromTransport(transport)
allTools.push(...serverTools)
} catch (error) {
console.error(`Failed to load tools from server ${serverName}:`, error)
// Continue with next server
}
}

return allTools
} catch (error) {
console.error("Failed to parse MCP config file:", error)
return []
}
}

private async getToolsFromTransport(transport: StdioClientTransport): Promise<any[]> {
Expand Down

0 comments on commit 1872cd8

Please sign in to comment.