Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/common/claudeMcpServerRegistry.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { McpServerConfig } from '@anthropic-ai/claude-agent-sdk';
7
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
8
9
/**
10
* Interface for contributing MCP server configurations to the Claude SDK Options.
11
*
12
* Implement this interface to register MCP servers that should be available
13
* during Claude Code sessions. Contributors are instantiated via dependency
14
* injection and can use any registered service.
15
*/
16
export interface IClaudeMcpServerContributor {
17
/**
18
* Returns MCP server configurations to include in the Claude SDK Options.
19
*
20
* @returns A record mapping server names to their configurations
21
*/
22
getMcpServers(): Promise<Record<string, McpServerConfig>>;
23
}
24
25
/**
26
* Constructor type for MCP server contributors.
27
* The instantiation service will handle dependency injection.
28
*/
29
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30
export type IClaudeMcpServerContributorCtor = new (...args: any[]) => IClaudeMcpServerContributor;
31
32
/**
33
* Global registry of MCP server contributor constructors.
34
*/
35
const contributorRegistry: IClaudeMcpServerContributorCtor[] = [];
36
37
/**
38
* Registers an MCP server contributor.
39
* Call this at module load time after defining a contributor class.
40
*
41
* @param ctor The constructor for the contributor class
42
*
43
* @example
44
* ```typescript
45
* export class MyMcpServers implements IClaudeMcpServerContributor {
46
* constructor(@IMyService private readonly myService: IMyService) { }
47
*
48
* async getMcpServers(): Promise<Record<string, McpServerConfig>> {
49
* return {
50
* 'my-server': { command: 'node', args: ['./server.js'] }
51
* };
52
* }
53
* }
54
*
55
* registerClaudeMcpServerContributor(MyMcpServers);
56
* ```
57
*/
58
export function registerClaudeMcpServerContributor(ctor: IClaudeMcpServerContributorCtor): void {
59
contributorRegistry.push(ctor);
60
}
61
62
/**
63
* Get all registered MCP server contributor constructors.
64
*/
65
export function getClaudeMcpServerContributorRegistry(): readonly IClaudeMcpServerContributorCtor[] {
66
return contributorRegistry;
67
}
68
69
/**
70
* Builds the mcpServers configuration from the registry using dependency injection.
71
* Instantiates each registered contributor and merges their server configurations.
72
*
73
* If multiple contributors provide a server with the same name, later registrations
74
* will overwrite earlier ones.
75
*
76
* @param instantiationService The instantiation service for creating contributor instances with DI
77
* @returns Merged MCP server configurations ready to pass to Claude SDK Options, or undefined if none
78
*/
79
export async function buildMcpServersFromRegistry(
80
instantiationService: IInstantiationService
81
): Promise<Record<string, McpServerConfig> | undefined> {
82
if (contributorRegistry.length === 0) {
83
return undefined;
84
}
85
86
const result: Record<string, McpServerConfig> = {};
87
88
for (const ctor of contributorRegistry) {
89
const contributor = instantiationService.createInstance(ctor);
90
const servers = await contributor.getMcpServers();
91
Object.assign(result, servers);
92
}
93
94
return Object.keys(result).length > 0 ? result : undefined;
95
}
96
97