fix: skip Anthropic MCP registry fetch for third-party providers (#310)
This commit is contained in:
61
src/services/mcp/officialRegistry.test.ts
Normal file
61
src/services/mcp/officialRegistry.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test'
|
||||
import axios from 'axios'
|
||||
|
||||
const originalEnv = { ...process.env }
|
||||
|
||||
async function importFreshModule() {
|
||||
return import(`./officialRegistry.ts?ts=${Date.now()}-${Math.random()}`)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
process.env = { ...originalEnv }
|
||||
})
|
||||
|
||||
describe('prefetchOfficialMcpUrls', () => {
|
||||
test('does not fetch registry when using OpenAI mode', async () => {
|
||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||
const getSpy = mock(() => Promise.resolve({ data: { servers: [] } }))
|
||||
axios.get = getSpy as typeof axios.get
|
||||
|
||||
const { prefetchOfficialMcpUrls } = await importFreshModule()
|
||||
await prefetchOfficialMcpUrls()
|
||||
|
||||
expect(getSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('does not fetch registry when using Gemini mode', async () => {
|
||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||
const getSpy = mock(() => Promise.resolve({ data: { servers: [] } }))
|
||||
axios.get = getSpy as typeof axios.get
|
||||
|
||||
const { prefetchOfficialMcpUrls } = await importFreshModule()
|
||||
await prefetchOfficialMcpUrls()
|
||||
|
||||
expect(getSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('fetches registry in first-party mode', async () => {
|
||||
delete process.env.CLAUDE_CODE_USE_OPENAI
|
||||
delete process.env.CLAUDE_CODE_USE_GEMINI
|
||||
delete process.env.CLAUDE_CODE_USE_GITHUB
|
||||
|
||||
const getSpy = mock(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
servers: [{ server: { remotes: [{ url: 'https://example.com/mcp' }] } }],
|
||||
},
|
||||
}),
|
||||
)
|
||||
axios.get = getSpy as typeof axios.get
|
||||
|
||||
const { prefetchOfficialMcpUrls, isOfficialMcpUrl } = await importFreshModule()
|
||||
await prefetchOfficialMcpUrls()
|
||||
|
||||
expect(getSpy).toHaveBeenCalledTimes(1)
|
||||
expect(isOfficialMcpUrl('https://example.com/mcp')).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,7 @@
|
||||
import axios from 'axios'
|
||||
import { logForDebugging } from '../../utils/debug.js'
|
||||
import { errorMessage } from '../../utils/errors.js'
|
||||
import { getAPIProvider } from '../../utils/model/providers.js'
|
||||
|
||||
type RegistryServer = {
|
||||
server: {
|
||||
@@ -35,6 +36,11 @@ export async function prefetchOfficialMcpUrls(): Promise<void> {
|
||||
return
|
||||
}
|
||||
|
||||
// Anthropic's official MCP registry is only relevant for first-party mode.
|
||||
if (getAPIProvider() !== 'firstParty') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get<RegistryResponse>(
|
||||
'https://api.anthropic.com/mcp-registry/v0/servers?version=latest&visibility=commercial',
|
||||
|
||||
Reference in New Issue
Block a user