代码模式
STDIO让LLM编写代码访问MCP服务器
让LLM编写代码访问MCP服务器
https://github.com/jx-codes/lootbox
It's a much more thought out approach to code mode
A local implementation of the "Code Mode" workflow for MCP servers. Instead of struggling with multiple tool calls, LLMs write TypeScript/JavaScript code that calls a simple HTTP proxy to access your MCP servers.
Note: It does not attempt to handle the MCP -> typescript API transpilation layer. Would be cool but I really wanted to test the workflow.
https://blog.cloudflare.com/code-mode/
This implements the core insight that LLMs are much better at writing code than at tool calling. Instead of exposing many tools directly to the LLM (which it struggles with), this server gives the LLM just one tool: execute_code. The LLM writes code that makes HTTP requests to access your other MCP servers.
execute_code - executes TypeScript/JavaScriptfetch() to call http://localhost:3001/mcp/* endpointsThis gives you all the benefits of complex tool orchestration, but leverages what LLMs are actually good at: writing code.
git clone https://github.com/jx-codes/codemode-mcp.git cd codemode-mcp
bun install
Create a codemode-config.json file to customize settings:
{ "proxyPort": 3001, "configDirectories": [ "~/.config/mcp/servers", "./mcp-servers", "./" ] }
Create a .mcp.json file with your MCP server configurations in any of the directories you specified above:
{ "mcpServers": { "fs": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], "env": {} } } }
Instead of direct tool calling, the LLM writes:
// List available servers const servers = await fetch("http://localhost:3001/mcp/servers").then((r) => r.json() ); console.log("Available servers:", servers); // Call a tool on the filesystem server const result = await fetch("http://localhost:3001/mcp/call", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ server: "fs", tool: "read_file", args: { path: "/tmp/example.txt" }, }), }).then((r) => r.json()); console.log("File contents:", result);
The real power shows when chaining operations:
// Get list of files const files = await fetch("http://localhost:3001/mcp/call", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ server: "fs", tool: "list_directory", args: { path: "/tmp" }, }), }).then((r) => r.json()); // Process each file for (const file of files.content[0].text.split("\n")) { if (file.endsWith(".txt")) { const content = await fetch("http://localhost:3001/mcp/call", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ server: "fs", tool: "read_file", args: { path: `/tmp/${file}` }, }), }).then((r) => r.json()); console.log(`${file}: ${content.content[0].text.length} characters`); } }
execute_codeExecutes TypeScript/JavaScript code with network access to the MCP proxy.
Parameters:
code (string): Code to executetypescript (boolean): TypeScript mode (default: true)Proxy Endpoints:
GET /mcp/servers - List available MCP serversGET /mcp/{server}/tools - List tools for serverPOST /mcp/call - Call tool (body: {server, tool, args})check_deno_versionCheck Deno installation status.
list_servers_with_toolsGet a comprehensive overview of all available MCP servers and their tools. Returns structured JSON data optimized for LLM consumption, containing complete tool schemas and server status information.
JSON Output Structure:
{ "summary": { "totalServers": 2, "successfulServers": 2, "totalTools": 4 }, "servers": [ { "server": "filesystem", "status": "success", "toolCount": 3, "tools": [ { "name": "read_file", "description": "Read contents of a file", "inputSchema": { "type": "object", "properties": { "path": { "type": "string", "description": "File path to read" } }, "required": ["path"] } } ] }, { "server": "database", "status": "success", "toolCount": 1, "tools": [ { "name": "query", "description": "Execute a SQL query", "inputSchema": { "type": "object", "properties": { "query": { "type": "string", "description": "SQL query to execute" } }, "required": ["query"] } } ] } ] }
This provides complete tool discovery information including parameter schemas, types, and requirements for programmatic access.
Create codemode-config.json:
{ "proxyPort": 3001, "configDirectories": ["~/.config/mcp/servers", "./mcp-servers", "./"] }
Add your MCP servers to .mcp.json files in those directories:
{ "mcpServers": { "fs": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], "env": {} } } }
Traditional MCP: LLM → Tool Call → MCP Server → Result → LLM → Tool Call → ...
Code Mode: LLM → Write Code → Code calls proxy → Proxy forwards to MCP → Results
"Deno not installed": Install Deno and restart
"Permission denied": Code trying to access restricted resources
"Module not found": Use https:// URLs for imports
"Execution timeout": Optimize code or break into smaller operations