feat: claude code mcp json format support
This commit is contained in:
parent
92c866f647
commit
f1253a74e1
3 changed files with 68 additions and 24 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import {
|
||||
claudeLikeConfigFileSchema,
|
||||
ConfigValidationError,
|
||||
convertJsonMcpConfigToYamlMcpConfig,
|
||||
McpJsonConfig,
|
||||
|
|
@ -15,6 +16,7 @@ import {
|
|||
DEFAULT_IGNORE_FILETYPES,
|
||||
} from "../../../indexing/ignore";
|
||||
import { walkDir } from "../../../indexing/walkDir";
|
||||
import { deduplicateArray } from "../../../util";
|
||||
import { getGlobalFolderWithName } from "../../../util/paths";
|
||||
import { localPathToUri } from "../../../util/pathToUri";
|
||||
import { getUriPathBasename, joinPathsToUri } from "../../../util/uri";
|
||||
|
|
@ -94,34 +96,49 @@ export async function loadJsonMcpConfigs(
|
|||
for (const { content, uri } of jsonFiles) {
|
||||
try {
|
||||
const json = JSONC.parse(content);
|
||||
const claudeFileParsed = mcpServerConfigFileSchema.safeParse(json);
|
||||
|
||||
// Try parsing as a file with mcpServers and multiple servers (claude-esque format)
|
||||
if (claudeFileParsed.success) {
|
||||
validJsonConfigs.push(
|
||||
...Object.entries(claudeFileParsed.data.mcpServers).map(
|
||||
([name, mcpJson]) => ({
|
||||
// Try parsing as a file with mcpServers and multiple servers (claude code/desktop-esque format)
|
||||
const claudeCodeFileParsed = claudeLikeConfigFileSchema.safeParse(json);
|
||||
if (claudeCodeFileParsed.success) {
|
||||
const projectServers = Object.values(
|
||||
claudeCodeFileParsed.data.projects,
|
||||
).map((v) => v.mcpServers);
|
||||
for (const mcpServers of projectServers) {
|
||||
validJsonConfigs.push(
|
||||
...Object.entries(mcpServers).map(([name, mcpJson]) => ({
|
||||
name,
|
||||
mcpJson,
|
||||
uri,
|
||||
}),
|
||||
),
|
||||
);
|
||||
claudeFileParsed.data;
|
||||
})),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Try parsing as single JSON file
|
||||
const singleConfigParsed = mcpServersJsonSchema.safeParse(json);
|
||||
if (singleConfigParsed.success) {
|
||||
validJsonConfigs.push({
|
||||
mcpJson: singleConfigParsed.data,
|
||||
name: getUriPathBasename(uri).replace(".json", ""),
|
||||
uri,
|
||||
});
|
||||
const claudeDesktopFileParsed =
|
||||
mcpServerConfigFileSchema.safeParse(json);
|
||||
if (claudeDesktopFileParsed.success) {
|
||||
validJsonConfigs.push(
|
||||
...Object.entries(claudeDesktopFileParsed.data.mcpServers).map(
|
||||
([name, mcpJson]) => ({
|
||||
name,
|
||||
mcpJson,
|
||||
uri,
|
||||
}),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
errors.push({
|
||||
fatal: false,
|
||||
message: `MCP JSON file at ${uri} doesn't match a supported MCP JSON configuration format`,
|
||||
});
|
||||
// Try parsing as single JSON file
|
||||
const singleConfigParsed = mcpServersJsonSchema.safeParse(json);
|
||||
if (singleConfigParsed.success) {
|
||||
validJsonConfigs.push({
|
||||
mcpJson: singleConfigParsed.data,
|
||||
name: getUriPathBasename(uri).replace(".json", ""),
|
||||
uri,
|
||||
});
|
||||
} else {
|
||||
errors.push({
|
||||
fatal: false,
|
||||
message: `MCP JSON file at ${uri} doesn't match a supported MCP JSON configuration format`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
|
@ -132,8 +149,14 @@ export async function loadJsonMcpConfigs(
|
|||
}
|
||||
}
|
||||
|
||||
// De-duplicate
|
||||
const deduplicatedJsonConfigs = deduplicateArray(
|
||||
validJsonConfigs,
|
||||
(a, b) => a.name === b.name,
|
||||
);
|
||||
|
||||
// Two levels of conversion for now.
|
||||
const yamlConfigs = validJsonConfigs.map((c) => {
|
||||
const yamlConfigs = deduplicatedJsonConfigs.map((c) => {
|
||||
const { warnings, yamlConfig } = convertJsonMcpConfigToYamlMcpConfig(
|
||||
c.name,
|
||||
c.mcpJson,
|
||||
|
|
|
|||
9
manual-testing-sandbox/claude_desktop_config.json
Normal file
9
manual-testing-sandbox/claude_desktop_config.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"mcpServers": {
|
||||
"linear": {
|
||||
"command": "npx",
|
||||
"args": ["-y", "mcp-remote", "https://mcp.linear.app/sse"],
|
||||
"envFile": "hey"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -36,3 +36,15 @@ export const mcpServerConfigFileSchema = z.object({
|
|||
export type McpServersJsonConfigFile = z.infer<
|
||||
typeof mcpServerConfigFileSchema
|
||||
>;
|
||||
|
||||
export const claudeLikeConfigFileSchema = z.object({
|
||||
projects: z.record(
|
||||
z.string(),
|
||||
z.object({
|
||||
mcpServers: mcpServersRecordSchema,
|
||||
}),
|
||||
),
|
||||
});
|
||||
export type ClaudeLikeConfigFileSchema = z.infer<
|
||||
typeof claudeLikeConfigFileSchema
|
||||
>;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue