This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 229
Add support for Claude MCP.json configuration files in CLI run command #1922
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { loadClaudeMcpConfig } from "../src/mcp-config" | ||
| import { writeJSON, readJSON } from "fs-extra" | ||
| import { resolve } from "node:path" | ||
| import { tmpdir } from "node:os" | ||
| import { mkdtemp, rm } from "node:fs/promises" | ||
|
|
||
| describe("MCP Configuration Loading", () => { | ||
| let tempDir: string | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = await mkdtemp(resolve(tmpdir(), "genaiscript-mcp-test-")) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await rm(tempDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| test("should load basic MCP configuration", async () => { | ||
| const configPath = resolve(tempDir, "mcp.json") | ||
| const config = { | ||
| servers: { | ||
| filesystem: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-filesystem"] | ||
| }, | ||
| memory: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-memory"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await writeJSON(configPath, config) | ||
| const result = await loadClaudeMcpConfig(configPath) | ||
|
|
||
| expect(result).toEqual({ | ||
| filesystem: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-filesystem"], | ||
| env: undefined, | ||
| cwd: undefined | ||
| }, | ||
| memory: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-memory"], | ||
| env: undefined, | ||
| cwd: undefined | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| test("should interpolate workspaceFolder variable", async () => { | ||
| const configPath = resolve(tempDir, "mcp.json") | ||
| const workspaceFolder = "/test/workspace" | ||
| const config = { | ||
| servers: { | ||
| filesystem: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-filesystem", "${workspaceFolder}"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await writeJSON(configPath, config) | ||
| const result = await loadClaudeMcpConfig(configPath, workspaceFolder) | ||
|
|
||
| expect(result.filesystem.args).toEqual([ | ||
| "-y", | ||
| "@modelcontextprotocol/server-filesystem", | ||
| workspaceFolder | ||
| ]) | ||
| }) | ||
|
|
||
| test("should interpolate environment variables", async () => { | ||
| const configPath = resolve(tempDir, "mcp.json") | ||
| const config = { | ||
| servers: { | ||
| test: { | ||
| command: "test", | ||
| env: { | ||
| "DEBUG": "${env:TEST_DEBUG}", | ||
| "PATH": "${env:PATH}" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Set test environment variable | ||
| process.env.TEST_DEBUG = "true" | ||
|
|
||
| await writeJSON(configPath, config) | ||
| const result = await loadClaudeMcpConfig(configPath) | ||
|
|
||
| expect(result.test.env.DEBUG).toBe("true") | ||
| expect(result.test.env.PATH).toBe(process.env.PATH) | ||
| }) | ||
|
|
||
| test("should handle missing configuration file", async () => { | ||
| const nonExistentPath = resolve(tempDir, "missing.json") | ||
|
|
||
| await expect(loadClaudeMcpConfig(nonExistentPath)).rejects.toThrow( | ||
| /MCP configuration file not found/ | ||
| ) | ||
| }) | ||
|
|
||
| test("should handle invalid JSON", async () => { | ||
| const configPath = resolve(tempDir, "invalid.json") | ||
| await writeJSON(configPath, "invalid json content") | ||
|
|
||
| await expect(loadClaudeMcpConfig(configPath)).rejects.toThrow( | ||
| /Failed to parse MCP configuration file/ | ||
| ) | ||
| }) | ||
|
|
||
| test("should handle missing servers object", async () => { | ||
| const configPath = resolve(tempDir, "no-servers.json") | ||
| const config = { other: "data" } | ||
|
|
||
| await writeJSON(configPath, config) | ||
|
|
||
| await expect(loadClaudeMcpConfig(configPath)).rejects.toThrow( | ||
| /Invalid MCP configuration: missing or invalid 'servers' object/ | ||
| ) | ||
| }) | ||
|
|
||
| test("should use config file directory as default workspace folder", async () => { | ||
| const configPath = resolve(tempDir, "mcp.json") | ||
| const config = { | ||
| servers: { | ||
| filesystem: { | ||
| command: "npx", | ||
| args: ["-y", "@modelcontextprotocol/server-filesystem", "${workspaceFolder}"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await writeJSON(configPath, config) | ||
| const result = await loadClaudeMcpConfig(configPath) | ||
|
|
||
| expect(result.filesystem.args[2]).toBe(tempDir) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { readJSON } from "fs-extra" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use genaiscriptDebug and add debug statements in the file to trace the mcp server resolution
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| import { resolve, dirname } from "node:path" | ||
| import { existsSync } from "node:fs" | ||
| import { genaiscriptDebug } from "../../core/src/debug" | ||
|
|
||
| const dbg = genaiscriptDebug("mcp:config") | ||
|
|
||
| /** | ||
| * Claude MCP configuration file format | ||
| */ | ||
| interface ClaudeMcpConfig { | ||
| servers?: Record<string, ClaudeMcpServerConfig> | ||
| mcpServers?: Record<string, ClaudeMcpServerConfig> | ||
| } | ||
|
|
||
| interface ClaudeMcpServerConfig { | ||
| type?: "stdio" | ||
| command: string | ||
| args?: string[] | ||
| env?: Record<string, string> | ||
| envFile?: string | ||
| cwd?: string | ||
| } | ||
|
|
||
| /** | ||
| * Interpolates Claude environment variables in a string | ||
| * Supports ${workspaceFolder}, ${env:VARIABLE_NAME}, ${VARIABLE_NAME} (for capitalized env vars), etc. | ||
| */ | ||
| function interpolateClaudeVariables( | ||
| value: string, | ||
| workspaceFolder: string, | ||
| env: Record<string, string> = process.env | ||
| ): string { | ||
| return value | ||
| .replace(/\$\{workspaceFolder\}/g, workspaceFolder) | ||
| .replace(/\$\{env:([^}]+)\}/g, (_, varName) => env[varName] || "") | ||
| .replace(/\$\{([A-Z_][A-Z0-9_]*)\}/g, (_, varName) => env[varName] || "") | ||
| } | ||
|
|
||
| /** | ||
| * Recursively interpolates Claude variables in an object | ||
| */ | ||
| function interpolateObjectValues( | ||
| obj: any, | ||
| workspaceFolder: string, | ||
| env: Record<string, string> = process.env | ||
| ): any { | ||
| if (typeof obj === "string") { | ||
| return interpolateClaudeVariables(obj, workspaceFolder, env) | ||
| } | ||
| if (Array.isArray(obj)) { | ||
| return obj.map((item) => interpolateObjectValues(item, workspaceFolder, env)) | ||
| } | ||
| if (obj && typeof obj === "object") { | ||
| const result: any = {} | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| result[key] = interpolateObjectValues(value, workspaceFolder, env) | ||
| } | ||
| return result | ||
| } | ||
| return obj | ||
| } | ||
|
|
||
| /** | ||
| * Loads and parses a Claude MCP configuration file | ||
| * @param configPath Path to the MCP configuration file | ||
| * @param workspaceFolder Workspace folder for variable interpolation (defaults to config file directory) | ||
| * @returns Parsed MCP server configurations | ||
| */ | ||
| export async function loadClaudeMcpConfig( | ||
| configPath: string, | ||
| workspaceFolder?: string | ||
| ): Promise<Record<string, any>> { | ||
| const resolvedPath = resolve(configPath) | ||
|
|
||
| dbg(`Loading MCP configuration from: ${resolvedPath}`) | ||
|
|
||
| if (!existsSync(resolvedPath)) { | ||
| throw new Error(`MCP configuration file not found: ${resolvedPath}`) | ||
| } | ||
|
|
||
| let config: ClaudeMcpConfig | ||
| try { | ||
| config = await readJSON(resolvedPath) | ||
| dbg(`Successfully parsed MCP configuration file`) | ||
| } catch (error) { | ||
| dbg(`Failed to parse MCP configuration file: ${error.message}`) | ||
| throw new Error(`Failed to parse MCP configuration file: ${error.message}`) | ||
| } | ||
|
|
||
| // Support both "servers" and "mcpServers" key names | ||
| const serversConfig = config.servers || config.mcpServers | ||
| if (!serversConfig || typeof serversConfig !== "object") { | ||
| throw new Error("Invalid MCP configuration: missing or invalid 'servers' or 'mcpServers' object") | ||
| } | ||
|
|
||
| // Use config file directory as workspace folder if not provided | ||
| const wsFolder = workspaceFolder || dirname(resolvedPath) | ||
| dbg(`Using workspace folder: ${wsFolder}`) | ||
|
|
||
| // Convert Claude format to GenAIScript format | ||
| const mcpServers: Record<string, any> = {} | ||
|
|
||
| for (const [serverId, serverConfig] of Object.entries(serversConfig)) { | ||
| dbg(`Processing server: ${serverId}`) | ||
|
|
||
| // Interpolate variables in the server configuration | ||
| const interpolatedConfig = interpolateObjectValues(serverConfig, wsFolder) | ||
|
|
||
| dbg(`Interpolated config for ${serverId}:`, interpolatedConfig) | ||
|
|
||
| // Convert to GenAIScript McpServerConfig format | ||
| const genaiscriptConfig = { | ||
| command: interpolatedConfig.command, | ||
| args: interpolatedConfig.args || [], | ||
| env: interpolatedConfig.env, | ||
| cwd: interpolatedConfig.cwd | ||
| } | ||
|
|
||
| mcpServers[serverId] = genaiscriptConfig | ||
| } | ||
|
|
||
| dbg(`Loaded ${Object.keys(mcpServers).length} MCP servers:`, Object.keys(mcpServers)) | ||
|
|
||
| return mcpServers | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot also support ${VARIABLE_NAME} to resolve a env variable (must be capitalized)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added support for
${VARIABLE_NAME}syntax to resolve capitalized environment variables in addition to the existing${env:VARIABLE_NAME}format. Updated both code and documentation. Changes in commit 064492a.