|
1 | 1 | import { listRepos } from '@/app/api/(server)/repos/listReposApi'; |
2 | | -import { getConfiguredLanguageModelsInfo } from "../chat/utils.server"; |
| 2 | +import { getConfiguredLanguageModels, getConfiguredLanguageModelsInfo } from "../chat/utils.server"; |
3 | 3 | import { askCodebase } from '@/features/mcp/askCodebase'; |
4 | 4 | import { |
5 | 5 | languageModelInfoSchema, |
@@ -66,12 +66,15 @@ const TOOL_DESCRIPTIONS = { |
66 | 66 | `, |
67 | 67 | }; |
68 | 68 |
|
69 | | -export function createMcpServer(): McpServer { |
| 69 | +export async function createMcpServer(): Promise<McpServer> { |
70 | 70 | const server = new McpServer({ |
71 | 71 | name: 'sourcebot-mcp-server', |
72 | 72 | version: SOURCEBOT_VERSION, |
73 | 73 | }); |
74 | 74 |
|
| 75 | + const configuredModels = await getConfiguredLanguageModels(); |
| 76 | + const hasLanguageModels = configuredModels.length > 0; |
| 77 | + |
75 | 78 | server.registerTool( |
76 | 79 | "search_code", |
77 | 80 | { |
@@ -493,43 +496,45 @@ export function createMcpServer(): McpServer { |
493 | 496 | } |
494 | 497 | ); |
495 | 498 |
|
496 | | - server.registerTool( |
497 | | - "ask_codebase", |
498 | | - { |
499 | | - description: TOOL_DESCRIPTIONS.ask_codebase, |
500 | | - annotations: { readOnlyHint: true }, |
501 | | - inputSchema: z.object({ |
502 | | - query: z.string().describe("The query to ask about the codebase."), |
503 | | - repos: z.array(z.string()).optional().describe("The repositories accessible to the agent. If not provided, all repositories are accessible."), |
504 | | - languageModel: languageModelInfoSchema.optional().describe("The language model to use. If not provided, defaults to the first model in the config."), |
505 | | - visibility: z.enum(['PRIVATE', 'PUBLIC']).optional().describe("The visibility of the chat session. Defaults to PRIVATE for authenticated users."), |
506 | | - }), |
507 | | - }, |
508 | | - async (request) => { |
509 | | - const result = await askCodebase({ |
510 | | - query: request.query, |
511 | | - repos: request.repos, |
512 | | - languageModel: request.languageModel, |
513 | | - visibility: request.visibility as ChatVisibility | undefined, |
514 | | - source: 'mcp', |
515 | | - }); |
| 499 | + if (hasLanguageModels) { |
| 500 | + server.registerTool( |
| 501 | + "ask_codebase", |
| 502 | + { |
| 503 | + description: TOOL_DESCRIPTIONS.ask_codebase, |
| 504 | + annotations: { readOnlyHint: true }, |
| 505 | + inputSchema: z.object({ |
| 506 | + query: z.string().describe("The query to ask about the codebase."), |
| 507 | + repos: z.array(z.string()).optional().describe("The repositories accessible to the agent. If not provided, all repositories are accessible."), |
| 508 | + languageModel: languageModelInfoSchema.optional().describe("The language model to use. If not provided, defaults to the first model in the config."), |
| 509 | + visibility: z.enum(['PRIVATE', 'PUBLIC']).optional().describe("The visibility of the chat session. Defaults to PRIVATE for authenticated users."), |
| 510 | + }), |
| 511 | + }, |
| 512 | + async (request) => { |
| 513 | + const result = await askCodebase({ |
| 514 | + query: request.query, |
| 515 | + repos: request.repos, |
| 516 | + languageModel: request.languageModel, |
| 517 | + visibility: request.visibility as ChatVisibility | undefined, |
| 518 | + source: 'mcp', |
| 519 | + }); |
516 | 520 |
|
517 | | - if (isServiceError(result)) { |
518 | | - return { |
519 | | - content: [{ type: "text", text: `Failed to ask codebase: ${result.message}` }], |
520 | | - }; |
521 | | - } |
| 521 | + if (isServiceError(result)) { |
| 522 | + return { |
| 523 | + content: [{ type: "text", text: `Failed to ask codebase: ${result.message}` }], |
| 524 | + }; |
| 525 | + } |
522 | 526 |
|
523 | | - const formattedResponse = dedent` |
524 | | - ${result.answer} |
| 527 | + const formattedResponse = dedent` |
| 528 | + ${result.answer} |
525 | 529 |
|
526 | | - --- |
527 | | - **View full research session:** ${result.chatUrl} |
528 | | - **Model used:** ${result.languageModel.model} |
529 | | - `; |
530 | | - return { content: [{ type: "text", text: formattedResponse }] }; |
531 | | - } |
532 | | - ); |
| 530 | + --- |
| 531 | + **View full research session:** ${result.chatUrl} |
| 532 | + **Model used:** ${result.languageModel.model} |
| 533 | + `; |
| 534 | + return { content: [{ type: "text", text: formattedResponse }] }; |
| 535 | + } |
| 536 | + ); |
| 537 | + } |
533 | 538 |
|
534 | 539 | return server; |
535 | 540 | } |
0 commit comments