-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add support for using MCP_CLI_CONFIG env var for config file, and improve error handling #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
technicalpickles
wants to merge
7
commits into
wong2:main
Choose a base branch
from
technicalpickles:feat/config-env-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25fca99
feat: enhance --config with MCP_CLI_CONFIG env var and better error h…
technicalpickles 16f9bdb
docs: add MCP_CLI_CONFIG env var and enhanced config documentation
technicalpickles 89aa6fd
refactor: extract config logic to dedicated module
technicalpickles 03e69fc
refactor: simplify config API and reduce code complexity
technicalpickles 06eb9af
refactor: move env var logic into getDefaultConfigPath
technicalpickles 9f4d437
Rename getDefaultConfigPath to resolveConfigPath
technicalpickles a1abe6a
refactor: replace manual config validation with Zod schema
technicalpickles 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,121 @@ | ||
import { existsSync } from 'node:fs' | ||
import { readFile } from 'node:fs/promises' | ||
import { homedir } from 'os' | ||
import { join } from 'path' | ||
import prompts from 'prompts' | ||
import { createSpinner } from '../utils.js' | ||
import { ConfigSchema, formatZodError } from './schema.js' | ||
|
||
function resolveConfigPath(cliConfigPath) { | ||
// Priority: CLI arg → env var → platform default | ||
if (cliConfigPath) { | ||
return cliConfigPath | ||
} | ||
|
||
const envConfigPath = process.env.MCP_CLI_CONFIG | ||
if (envConfigPath) { | ||
return envConfigPath | ||
} | ||
|
||
if (process.platform === 'win32') { | ||
return join(homedir(), 'AppData', 'Roaming', 'Claude', 'claude_desktop_config.json') | ||
} | ||
if (process.platform === 'darwin') { | ||
return join(homedir(), 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json') | ||
} | ||
} | ||
|
||
function validateConfig(config, configFilePath) { | ||
const result = ConfigSchema.safeParse(config) | ||
|
||
if (!result.success) { | ||
throw new Error(formatZodError(result.error, configFilePath)) | ||
} | ||
|
||
return result.data | ||
} | ||
|
||
export async function loadConfig(configPath, { silent = false } = {}) { | ||
const resolvedPath = resolveConfigPath(configPath) | ||
|
||
if (!resolvedPath) { | ||
throw new Error('No config file path provided') | ||
} | ||
|
||
if (!existsSync(resolvedPath)) { | ||
throw new Error(`Config file not found: ${resolvedPath} | ||
Please check that the file exists and you have read permissions.`) | ||
} | ||
|
||
let spinner | ||
if (!silent) { | ||
spinner = createSpinner(`Loading config from ${resolvedPath}`) | ||
} | ||
|
||
try { | ||
const configContent = await readFile(resolvedPath, 'utf-8') | ||
|
||
if (!configContent.trim()) { | ||
throw new Error(`Config file contains no data: ${resolvedPath}`) | ||
} | ||
|
||
let config | ||
try { | ||
config = JSON.parse(configContent) | ||
} catch (parseError) { | ||
let errorMessage = `Invalid JSON in config file: ${resolvedPath}\n` | ||
|
||
// Add line/column info if available | ||
if (parseError.message.includes('Unexpected token')) { | ||
const match = parseError.message.match(/position (\d+)/) | ||
if (match) { | ||
const position = parseInt(match[1]) | ||
const lines = configContent.substring(0, position).split('\n') | ||
const lineNumber = lines.length | ||
const columnNumber = lines[lines.length - 1].length + 1 | ||
errorMessage += `Error at line ${lineNumber}, column ${columnNumber}\n` | ||
} | ||
} | ||
|
||
errorMessage += `Common issues: missing commas, trailing commas, unquoted property names` | ||
throw new Error(errorMessage) | ||
} | ||
|
||
const validatedConfig = validateConfig(config, resolvedPath) | ||
|
||
if (spinner) { | ||
spinner.success() | ||
} | ||
|
||
return validatedConfig | ||
} catch (error) { | ||
if (spinner) { | ||
spinner.error(`Failed to load config: ${error.message}`) | ||
} | ||
|
||
// Handle file system errors | ||
if (error.code === 'EACCES') { | ||
throw new Error(`Permission denied reading config file: ${resolvedPath}`) | ||
} else if (error.code === 'ENOENT') { | ||
throw new Error(`Config file not found: ${resolvedPath}`) | ||
} else if (error.code === 'EISDIR') { | ||
throw new Error(`Expected file but found directory: ${resolvedPath}`) | ||
} | ||
|
||
throw error | ||
} | ||
} | ||
|
||
export async function pickServer(config) { | ||
const { server } = await prompts({ | ||
name: 'server', | ||
type: 'autocomplete', | ||
message: 'Pick a server', | ||
choices: Object.keys(config.mcpServers).map((s) => ({ | ||
title: s, | ||
value: s, | ||
})), | ||
}) | ||
return server | ||
} | ||
|
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,23 @@ | ||
import { z } from 'zod' | ||
|
||
const ServerConfigSchema = z.object({ | ||
command: z.string().min(1, 'Command cannot be empty'), | ||
args: z.array(z.string()).optional(), | ||
env: z.record(z.string()).optional() | ||
}) | ||
|
||
export const ConfigSchema = z.object({ | ||
mcpServers: z.record(ServerConfigSchema).refine( | ||
(servers) => Object.keys(servers).length > 0, | ||
{ message: 'mcpServers must contain at least one server configuration' } | ||
) | ||
}).strict() | ||
|
||
export function formatZodError(error, configFilePath) { | ||
const issues = error.errors.map(issue => { | ||
const path = issue.path.length > 0 ? issue.path.join('.') : 'root' | ||
return ` • ${path}: ${issue.message}` | ||
}).join('\n') | ||
|
||
return `Invalid configuration in ${configFilePath}:\n${issues}` | ||
} |
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
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.