Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:48:52 +08:00
commit 6ec3196ecc
434 changed files with 125248 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
# MCP Management Scripts Environment Variables
# Path to MCP configuration file (optional, defaults to .claude/.mcp.json)
MCP_CONFIG_PATH=.claude/.mcp.json
# Logging level (optional, defaults to info)
LOG_LEVEL=info
# Enable debug mode (optional, defaults to false)
DEBUG=false

View File

@@ -0,0 +1,155 @@
#!/usr/bin/env node
/**
* MCP Management CLI - Command-line interface for MCP operations
*/
import { MCPClientManager } from './mcp-client.js';
import { writeFileSync, mkdirSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function main() {
const args = process.argv.slice(2);
const command = args[0];
const manager = new MCPClientManager();
try {
// Load config
await manager.loadConfig();
console.log('✓ Config loaded');
// Connect to all servers
await manager.connectAll();
console.log('✓ Connected to all MCP servers\n');
switch (command) {
case 'list-tools':
await listTools(manager);
break;
case 'list-prompts':
await listPrompts(manager);
break;
case 'list-resources':
await listResources(manager);
break;
case 'call-tool':
await callTool(manager, args[1], args[2], args[3]);
break;
default:
printUsage();
}
await manager.cleanup();
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
async function listTools(manager: MCPClientManager) {
const tools = await manager.getAllTools();
console.log(`Found ${tools.length} tools:\n`);
for (const tool of tools) {
console.log(`📦 ${tool.serverName} / ${tool.name}`);
console.log(` ${tool.description}`);
if (tool.inputSchema?.properties) {
console.log(` Parameters: ${Object.keys(tool.inputSchema.properties).join(', ')}`);
}
console.log('');
}
// Save tools to JSON file
const assetsDir = join(__dirname, '..', 'assets');
const toolsPath = join(assetsDir, 'tools.json');
try {
mkdirSync(assetsDir, { recursive: true });
writeFileSync(toolsPath, JSON.stringify(tools, null, 2));
console.log(`\n✓ Tools saved to ${toolsPath}`);
} catch (error) {
console.error(`\n✗ Failed to save tools: ${error}`);
}
}
async function listPrompts(manager: MCPClientManager) {
const prompts = await manager.getAllPrompts();
console.log(`Found ${prompts.length} prompts:\n`);
for (const prompt of prompts) {
console.log(`💬 ${prompt.serverName} / ${prompt.name}`);
console.log(` ${prompt.description}`);
if (prompt.arguments && prompt.arguments.length > 0) {
console.log(` Arguments: ${prompt.arguments.map((a: any) => a.name).join(', ')}`);
}
console.log('');
}
}
async function listResources(manager: MCPClientManager) {
const resources = await manager.getAllResources();
console.log(`Found ${resources.length} resources:\n`);
for (const resource of resources) {
console.log(`📄 ${resource.serverName} / ${resource.name}`);
console.log(` URI: ${resource.uri}`);
if (resource.description) {
console.log(` ${resource.description}`);
}
if (resource.mimeType) {
console.log(` Type: ${resource.mimeType}`);
}
console.log('');
}
}
async function callTool(
manager: MCPClientManager,
serverName: string,
toolName: string,
argsJson: string
) {
if (!serverName || !toolName || !argsJson) {
console.error('Usage: cli.ts call-tool <server> <tool> <json-args>');
process.exit(1);
}
const args = JSON.parse(argsJson);
console.log(`Calling ${serverName}/${toolName}...`);
const result = await manager.callTool(serverName, toolName, args);
console.log('\nResult:');
console.log(JSON.stringify(result, null, 2));
}
function printUsage() {
console.log(`
MCP Management CLI
Usage:
cli.ts <command> [options]
Commands:
list-tools List all tools and save to assets/tools.json
list-prompts List all prompts from all MCP servers
list-resources List all resources from all MCP servers
call-tool <server> <tool> <json> Call a specific tool
Examples:
cli.ts list-tools
cli.ts call-tool memory create_entities '{"entities":[{"name":"Alice","entityType":"person"}]}'
cli.ts call-tool human-mcp playwright_screenshot_fullpage '{"url":"https://example.com"}'
Note: Tool analysis is done by the LLM reading assets/tools.json directly.
`);
}
main();

View File

@@ -0,0 +1,163 @@
#!/usr/bin/env node
/**
* MCP Client - Core client for interacting with MCP servers
*/
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { readFile } from 'fs/promises';
import { resolve } from 'path';
interface MCPConfig {
mcpServers: {
[key: string]: {
command: string;
args: string[];
env?: Record<string, string>;
};
};
}
interface ToolInfo {
serverName: string;
name: string;
description: string;
inputSchema: any;
outputSchema?: any;
}
interface PromptInfo {
serverName: string;
name: string;
description: string;
arguments?: any[];
}
interface ResourceInfo {
serverName: string;
uri: string;
name: string;
description?: string;
mimeType?: string;
}
export class MCPClientManager {
private config: MCPConfig | null = null;
private clients: Map<string, Client> = new Map();
async loadConfig(configPath: string = '.claude/.mcp.json'): Promise<MCPConfig> {
const fullPath = resolve(process.cwd(), configPath);
const content = await readFile(fullPath, 'utf-8');
const config = JSON.parse(content) as MCPConfig;
this.config = config;
return config;
}
async connectToServer(serverName: string): Promise<Client> {
if (!this.config?.mcpServers[serverName]) {
throw new Error(`Server ${serverName} not found in config`);
}
const serverConfig = this.config.mcpServers[serverName];
const transport = new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args,
env: serverConfig.env
});
const client = new Client({
name: `mcp-manager-${serverName}`,
version: '1.0.0'
}, { capabilities: {} });
await client.connect(transport);
this.clients.set(serverName, client);
return client;
}
async connectAll(): Promise<void> {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig() first.');
}
const connections = Object.keys(this.config.mcpServers).map(name =>
this.connectToServer(name)
);
await Promise.all(connections);
}
async getAllTools(): Promise<ToolInfo[]> {
const allTools: ToolInfo[] = [];
for (const [serverName, client] of this.clients.entries()) {
const response = await client.listTools({}, { timeout: 300000 });
for (const tool of response.tools) {
allTools.push({
serverName,
name: tool.name,
description: tool.description || '',
inputSchema: tool.inputSchema,
outputSchema: (tool as any).outputSchema
});
}
}
return allTools;
}
async getAllPrompts(): Promise<PromptInfo[]> {
const allPrompts: PromptInfo[] = [];
for (const [serverName, client] of this.clients.entries()) {
const response = await client.listPrompts({}, { timeout: 300000 });
for (const prompt of response.prompts) {
allPrompts.push({
serverName,
name: prompt.name,
description: prompt.description || '',
arguments: prompt.arguments
});
}
}
return allPrompts;
}
async getAllResources(): Promise<ResourceInfo[]> {
const allResources: ResourceInfo[] = [];
for (const [serverName, client] of this.clients.entries()) {
const response = await client.listResources({}, { timeout: 300000 });
for (const resource of response.resources) {
allResources.push({
serverName,
uri: resource.uri,
name: resource.name,
description: resource.description,
mimeType: resource.mimeType
});
}
}
return allResources;
}
async callTool(serverName: string, toolName: string, args: any): Promise<any> {
const client = this.clients.get(serverName);
if (!client) throw new Error(`Not connected to server: ${serverName}`);
return await client.callTool({ name: toolName, arguments: args }, { timeout: 300000 });
}
async getPrompt(serverName: string, promptName: string, args?: any): Promise<any> {
const client = this.clients.get(serverName);
if (!client) throw new Error(`Not connected to server: ${serverName}`);
return await client.getPrompt({ name: promptName, arguments: args }, { timeout: 300000 });
}
async readResource(serverName: string, uri: string): Promise<any> {
const client = this.clients.get(serverName);
if (!client) throw new Error(`Not connected to server: ${serverName}`);
return await client.readResource({ uri }, { timeout: 300000 });
}
async cleanup(): Promise<void> {
for (const client of this.clients.values()) {
await client.close();
}
this.clients.clear();
}
}

View File

@@ -0,0 +1,18 @@
{
"name": "mcp-management-scripts",
"version": "1.0.0",
"type": "module",
"description": "MCP client scripts for managing MCP servers",
"scripts": {
"build": "tsc",
"test": "node --loader ts-node/esm test.ts"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0",
"ts-node": "^10.0.0"
}
}

View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./",
"resolveJsonModule": true
},
"include": ["*.ts"],
"exclude": ["node_modules", "dist"]
}