fix(mcp): lazy evaluation prevents crash when websearch disabled
createWebsearchConfig was called eagerly before checking disabledMcps, causing Tavily missing-key error even when websearch was disabled. Now each MCP is only created if not in disabledMcps list.
This commit is contained in:
parent
17cb49543a
commit
5a2ab0095d
@ -83,4 +83,22 @@ describe("createBuiltinMcps", () => {
|
|||||||
expect(result).toHaveProperty("grep_app")
|
expect(result).toHaveProperty("grep_app")
|
||||||
expect(Object.keys(result)).toHaveLength(3)
|
expect(Object.keys(result)).toHaveLength(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("should not throw when websearch disabled even if tavily configured without API key", () => {
|
||||||
|
// given
|
||||||
|
const originalTavilyKey = process.env.TAVILY_API_KEY
|
||||||
|
delete process.env.TAVILY_API_KEY
|
||||||
|
const disabledMcps = ["websearch"]
|
||||||
|
const config = { websearch: { provider: "tavily" as const } }
|
||||||
|
|
||||||
|
// when
|
||||||
|
const createMcps = () => createBuiltinMcps(disabledMcps, config)
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(createMcps).not.toThrow()
|
||||||
|
const result = createMcps()
|
||||||
|
expect(result).not.toHaveProperty("websearch")
|
||||||
|
|
||||||
|
if (originalTavilyKey) process.env.TAVILY_API_KEY = originalTavilyKey
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -15,18 +15,18 @@ type RemoteMcpConfig = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createBuiltinMcps(disabledMcps: string[] = [], config?: OhMyOpenCodeConfig) {
|
export function createBuiltinMcps(disabledMcps: string[] = [], config?: OhMyOpenCodeConfig) {
|
||||||
const allBuiltinMcps: Record<McpName, RemoteMcpConfig> = {
|
|
||||||
websearch: createWebsearchConfig(config?.websearch),
|
|
||||||
context7,
|
|
||||||
grep_app,
|
|
||||||
}
|
|
||||||
|
|
||||||
const mcps: Record<string, RemoteMcpConfig> = {}
|
const mcps: Record<string, RemoteMcpConfig> = {}
|
||||||
|
|
||||||
for (const [name, mcp] of Object.entries(allBuiltinMcps)) {
|
if (!disabledMcps.includes("websearch")) {
|
||||||
if (!disabledMcps.includes(name)) {
|
mcps.websearch = createWebsearchConfig(config?.websearch)
|
||||||
mcps[name] = mcp
|
}
|
||||||
}
|
|
||||||
|
if (!disabledMcps.includes("context7")) {
|
||||||
|
mcps.context7 = context7
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!disabledMcps.includes("grep_app")) {
|
||||||
|
mcps.grep_app = grep_app
|
||||||
}
|
}
|
||||||
|
|
||||||
return mcps
|
return mcps
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user