Show HN: Dynamic Code Execution with MCP: A More Efficient Approach

github.com

2 points by pranftw 10 hours ago

I've been working on a more efficient approach to code execution with MCP servers that eliminates the filesystem overhead described in Anthropic's recent blog post.

The Anthropic post (https://www.anthropic.com/engineering/code-execution-mcp) showed how agents can avoid token bloat by writing code to call MCP tools instead of using direct tool calls.

Their approach generates TypeScript files for each tool to enable progressive discovery. It works well but introduces complexity: you need to generate files for every tool, manage complex type schemas, rebuild when tools update, and handle version conflicts. At scale, 1000 MCP tools means maintaining 1000 generated files.

I built codex-mcp using pure dynamic execution. Instead of generating files, we expose just two lightweight tools: list_mcp_tools() returns available tool names, and get_mcp_tool_details(name) loads definitions on demand. The agent explores tools as if navigating a filesystem, but nothing actually exists on disk.

Code snippets are stored in-memory as strings in the chat session data. When you execute a snippet, we inject a callMCPTool function directly into the execution environment using AsyncFunction constructor. No imports, no filesystem dependencies, just runtime injection. The function calls mcpManager.tools directly, so you're always hitting the live MCP connection.

This means tools are perpetually in sync. When a tool's schema changes on the server, you're already calling the updated version. No regeneration, no build step, no version mismatches. The agent gets all the same benefits of the filesystem approach (progressive discovery, context efficiency, complex control flow, privacy preservation) without any of the maintenance overhead.

One caveat: the MCP protocol doesn't enforce output schemas, so chaining tool calls requires defensive parsing since the model can't predict output structure. This affects all MCP implementations though, not specific to our approach.

The dynamic execution is made possible by Vercel AI SDK's MCP support, which provides the runtime infrastructure to call MCP tools directly from code.

Project: https://github.com/pranftw/aiter-app

Would love feedback from folks working with MCP at scale. Has anyone else explored similar patterns?