Skip to content

Commit

Permalink
feat: refactor installMCPServer to support async execution and improv…
Browse files Browse the repository at this point in the history
…e command resolution

- Changed installMCPServer function to be asynchronous, allowing for better handling of command execution.
- Introduced execAsync to resolve the command for Python runtime dynamically.
- Updated package-management.ts to await the installMCPServer call, ensuring proper execution flow during package installation.

These changes enhance the installation process for MCP servers, particularly for Python packages, improving reliability and user experience.
  • Loading branch information
michaellatman committed Dec 3, 2024
1 parent 4e0dda3 commit cfe97b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import * as path from 'path';
import * as os from 'os';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const execAsync = promisify(exec);

export interface MCPServerConfig {
command: string;
args: string[];
Expand Down Expand Up @@ -58,7 +62,7 @@ export function writeConfig(config: ClaudeConfig): void {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}

export function installMCPServer(packageName: string, envVars?: Record<string, string>, runtime?: 'node' | 'python'): void {
export async function installMCPServer(packageName: string, envVars?: Record<string, string>, runtime?: 'node' | 'python'): Promise<void> {
const config = readConfig();
const serverName = packageName.replace(/\//g, '-');

Expand All @@ -67,11 +71,21 @@ export function installMCPServer(packageName: string, envVars?: Record<string, s
if (!config.mcpServers) {
config.mcpServers = {};
}

let command = 'npx';
if (effectiveRuntime === 'python') {
try {
const { stdout } = await execAsync('which uvx');
command = stdout.trim();
} catch (error) {
command = 'uvx'; // Fallback to just 'uvx' if which fails
}
}

const serverConfig: MCPServerConfig = {
runtime: effectiveRuntime,
env: envVars,
command: effectiveRuntime === 'python' ? 'uvx' : 'npx',
command,
args: effectiveRuntime === 'python' ? [packageName] : ['-y', packageName]
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/package-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export async function installPackage(pkg: Package): Promise<void> {

const envVars = await promptForEnvVars(pkg.name);

installMCPServer(pkg.name, envVars, pkg.runtime);
await installMCPServer(pkg.name, envVars, pkg.runtime);
console.log('Updated Claude desktop configuration');
await promptForRestart();
} catch (error) {
Expand Down

0 comments on commit cfe97b2

Please sign in to comment.