Elffuss BraidHub

kikocis/continue public

main · 4.8 KiBRawPermalink
1import type {
2 HttpMcpServer,
3 MCPServer,
4 SseMcpServer,
5 StdioMcpServer,
6} from "./index.js";
7import type {
8 HttpMcpJsonConfig,
9 McpJsonConfig,
10 McpServersJsonConfigFile,
11 SseMcpJsonConfig,
12} from "./json.js";
13
14/**
15 * Convert environment variable references from JSON format (${VAR}) to YAML format (${{ secrets.VAR }})
16 */
17export function convertJsonEnvToYamlEnv(
18 env: Record<string, string> | undefined,
19): Record<string, string> | undefined {
20 if (!env) return undefined;
21
22 return Object.fromEntries(
23 Object.entries(env).map(([key, value]) => [
24 key,
25 value.replace(/\$\{([^}]+)\}/g, "${{ secrets.$1 }}"),
26 ]),
27 );
28}
29
30/**
31 * Convert environment variable references from YAML format (${{ secrets.VAR }} or ${{ inputs.VAR }}) to JSON format (${VAR})
32 */
33export function convertYamlEnvToJsonEnv(
34 env: Record<string, string> | undefined,
35): Record<string, string> | undefined {
36 if (!env) return undefined;
37
38 return Object.fromEntries(
39 Object.entries(env).map(([key, value]) => [
40 key,
41 value.replace(/\$\{\{\s*(?:secrets|inputs)\.([^}\s]+)\s*\}\}/g, "${$1}"),
42 ]),
43 );
44}
45
46/**
47 * Convert from JSON schema (used in Claude Desktop) to YAML schema (used in Continue)
48 */
49export function convertJsonMcpConfigToYamlMcpConfig(
50 name: string,
51 jsonConfig: McpJsonConfig,
52): {
53 yamlConfig: MCPServer;
54 warnings: string[];
55} {
56 const warnings: string[] = [];
57
58 // STDIO
59 if ("command" in jsonConfig) {
60 if (jsonConfig.envFile) {
61 warnings.push(
62 `envFile is not supported in Continue MCP config (server "${name}"). Environment variables from this file will not be used.`,
63 );
64 }
65
66 const stdioConfig: StdioMcpServer = {
67 name,
68 type: "stdio",
69 command: jsonConfig.command,
70 args: jsonConfig.args,
71 env: convertJsonEnvToYamlEnv(jsonConfig.env),
72 };
73 return {
74 warnings,
75 yamlConfig: stdioConfig,
76 };
77 }
78
79 // SSE/HTTP
80 if ("url" in jsonConfig) {
81 const sseOrHttpConfig: SseMcpServer | HttpMcpServer = {
82 name,
83 url: jsonConfig.url,
84 };
85
86 if (jsonConfig.type) {
87 sseOrHttpConfig.type =
88 jsonConfig.type === "http" ? "streamable-http" : "sse";
89 }
90
91 if (jsonConfig.headers) {
92 sseOrHttpConfig.requestOptions = {
93 headers: jsonConfig.headers,
94 };
95 }
96
97 return {
98 warnings,
99 yamlConfig: sseOrHttpConfig,
100 };
101 }
102
103 throw new Error(`Invalid MCP server configuration`);
104}
105
106/**
107 * Convert from YAML schema (used in Continue) to JSON schema (e.g. used in Claude Desktop)
108 */
109export function convertYamlMcpConfigToJsonMcpConfig(yamlConfig: MCPServer): {
110 name: string;
111 jsonConfig: McpJsonConfig;
112 MCP_TIMEOUT?: string;
113 warnings: string[];
114} {
115 const { name, faviconUrl } = yamlConfig;
116
117 const warnings: string[] = [];
118 if (faviconUrl) {
119 warnings.push(
120 `\`faviconUrl\` from YAML MCP config not supported in Claude-style JSON, will be removed from server ${name}`,
121 );
122 }
123
124 // Claude uses MCP_TIMEOUT env variable rather than a configuration for stdio
125 const MCP_TIMEOUT = yamlConfig.connectionTimeout?.toString();
126
127 // STDIO
128 if ("command" in yamlConfig) {
129 const { command, args, env, cwd } = yamlConfig;
130
131 if (cwd) {
132 warnings.push(
133 `\`cwd\` from YAML MCP config not supported in Claude-style JSON, will be removed from server ${name}`,
134 );
135 }
136
137 return {
138 name,
139 MCP_TIMEOUT,
140 warnings,
141 jsonConfig: {
142 type: "stdio",
143 command,
144 args,
145 env: convertYamlEnvToJsonEnv(env),
146 },
147 };
148 }
149
150 // SSE/HTTP
151 if ("url" in yamlConfig) {
152 const { url, requestOptions } = yamlConfig;
153
154 const { headers, ...unsupportedReqOptions } = requestOptions ?? {};
155 for (const key of Object.keys(unsupportedReqOptions)) {
156 warnings.push(
157 `${key} requestOption from YAML MCP config not supported in Claude-style JSON, will be ignored in server ${name}`,
158 );
159 }
160
161 const httpOrSseJsonConfig: HttpMcpJsonConfig | SseMcpJsonConfig = {
162 url,
163 headers,
164 };
165
166 if (yamlConfig.type) {
167 httpOrSseJsonConfig.type =
168 yamlConfig.type === "streamable-http" ? "http" : "sse";
169 }
170
171 return {
172 name,
173 warnings,
174 jsonConfig: httpOrSseJsonConfig,
175 MCP_TIMEOUT,
176 };
177 }
178
179 throw new Error(`Invalid MCP server configuration`);
180}
181
182export function converMcpServersJsonConfigFileToYamlBlocks(
183 jsonFile: McpServersJsonConfigFile,
184): {
185 yamlConfigs: MCPServer[];
186 warnings: string[];
187} {
188 const allWarnings: string[] = [];
189 const jsonEntries = Object.entries(jsonFile.mcpServers ?? {});
190 const yamlConfigs = jsonEntries.map(([name, config]) => {
191 const { warnings, yamlConfig } = convertJsonMcpConfigToYamlMcpConfig(
192 name,
193 config,
194 );
195 allWarnings.push(...warnings);
196 return yamlConfig;
197 });
198
199 return {
200 warnings: allWarnings,
201 yamlConfigs,
202 };
203}