Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/claudeMcpServerRegistry.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { McpServerConfig } from '@anthropic-ai/claude-agent-sdk';6import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';78/**9* Interface for contributing MCP server configurations to the Claude SDK Options.10*11* Implement this interface to register MCP servers that should be available12* during Claude Code sessions. Contributors are instantiated via dependency13* injection and can use any registered service.14*/15export interface IClaudeMcpServerContributor {16/**17* Returns MCP server configurations to include in the Claude SDK Options.18*19* @returns A record mapping server names to their configurations20*/21getMcpServers(): Promise<Record<string, McpServerConfig>>;22}2324/**25* Constructor type for MCP server contributors.26* The instantiation service will handle dependency injection.27*/28// eslint-disable-next-line @typescript-eslint/no-explicit-any29export type IClaudeMcpServerContributorCtor = new (...args: any[]) => IClaudeMcpServerContributor;3031/**32* Global registry of MCP server contributor constructors.33*/34const contributorRegistry: IClaudeMcpServerContributorCtor[] = [];3536/**37* Registers an MCP server contributor.38* Call this at module load time after defining a contributor class.39*40* @param ctor The constructor for the contributor class41*42* @example43* ```typescript44* export class MyMcpServers implements IClaudeMcpServerContributor {45* constructor(@IMyService private readonly myService: IMyService) { }46*47* async getMcpServers(): Promise<Record<string, McpServerConfig>> {48* return {49* 'my-server': { command: 'node', args: ['./server.js'] }50* };51* }52* }53*54* registerClaudeMcpServerContributor(MyMcpServers);55* ```56*/57export function registerClaudeMcpServerContributor(ctor: IClaudeMcpServerContributorCtor): void {58contributorRegistry.push(ctor);59}6061/**62* Get all registered MCP server contributor constructors.63*/64export function getClaudeMcpServerContributorRegistry(): readonly IClaudeMcpServerContributorCtor[] {65return contributorRegistry;66}6768/**69* Builds the mcpServers configuration from the registry using dependency injection.70* Instantiates each registered contributor and merges their server configurations.71*72* If multiple contributors provide a server with the same name, later registrations73* will overwrite earlier ones.74*75* @param instantiationService The instantiation service for creating contributor instances with DI76* @returns Merged MCP server configurations ready to pass to Claude SDK Options, or undefined if none77*/78export async function buildMcpServersFromRegistry(79instantiationService: IInstantiationService80): Promise<Record<string, McpServerConfig> | undefined> {81if (contributorRegistry.length === 0) {82return undefined;83}8485const result: Record<string, McpServerConfig> = {};8687for (const ctor of contributorRegistry) {88const contributor = instantiationService.createInstance(ctor);89const servers = await contributor.getMcpServers();90Object.assign(result, servers);91}9293return Object.keys(result).length > 0 ? result : undefined;94}959697