Published: November 9, 2025
Stay in the loop, follow us on LinkedIn to catch fresh articles every week.
If you're looking to get started with Generative AI, Agentic AI workflows, or migrating your workloads to Microsoft Fabric, partnering with Tech-Insight-Group LLC is your strategic gateway to expert-led consulting and hands-on training services tailored for real-world impact.
This article was born out of a real-world challenge my team and I faced with a customer over the past two weeks. Despite our best efforts; including internal troubleshooting, leveraging Copilot, and consulting peers; we struggled to help the customer optimize costs and maintain strict data privacy within their MCP-based agent workflows. The turning point came when I discovered an insightful article published on Nov 04, 2025 from Anthropic that introduced a new paradigm: Code Execution with MCP.
It offered not just a technical solution, but a conceptual shift in how agents interact with tools; one that directly addressed the limitations we were up against. I’m sharing this here because I know others are likely facing similar constraints, and this approach could be the breakthrough they need. What follows is a practical, field-tested breakdown of how Code Execution with MCP works, why it matters, and how to apply it effectively.
To begin, let’s define what MCP entails. The Model Context Protocol (MCP) is a pattern for structuring AI agents by injecting tools, memory, and system prompts into a shared context window. It’s been widely adopted across frameworks like LangChain, CrewAI, Semantic Kernel, Microsoft Autogen, and even Claude’s Skills.
MCP enhances enterprise AI workflows by embedding tools directly into the model’s context, making them dynamically discoverable and immediately actionable. This contextual tooling empowers agents to operate with flexible, swappable components like tools, prompts, and memory without hardcoding logic, which accelerates iteration and reduces engineering overhead. Its support for multi-agent coordination allows shared context across agents, enabling collaborative reasoning and seamless handoffs in complex business processes like claims handling, customer support, or procurement workflows.
While MCP offers structural clarity, its implementation introduces significant drawbacks for enterprise AI workflows. Injecting full tool specs and memory into every prompt consumes up to 60–70% of the model’s context window, limiting reasoning depth and inflating compute costs. Frameworks like LangChain further obscure execution flow, complicating debugging and slowing performance.
Below is an example illustrating how agents can become overwhelmed by excessive prompt injections and context stacking. As tool definitions, memory buffers, and message history accumulate in the context window, the agent’s reasoning becomes prone to misalignment, inefficiency, and increased risk of prompt injection. This architecture highlights the fragility of context-based orchestration when scaled across complex workflows.
More critically, placing tool definitions in context exposes agents to prompt injection risks, raising governance and security concerns. These limitations have led many AI engineers to reconsider MCP’s role in scalable agent design. As orchestration grows more opaque and expensive, the industry is shifting toward approaches that prioritize clarity, efficiency, and modular control.
As code execution environments become increasingly common for AI agents, a highly effective solution is to expose MCP (Model Context Protocol) servers as code APIs instead of direct tool calls.
Code Execution with MCP is a modern approach to building AI agents that interact with external tools and systems using the Model Context Protocol (MCP). Instead of relying on direct tool calls embedded in the model’s context window, agents write and execute code (e.g., TypeScript or Python) to interact with MCP servers.
One practical method is to generate a file tree of all available tools from connected MCP servers, making them easily discoverable and callable. This approach streamlines orchestration, enhances modularity, and sets the stage for scalable, low-latency agent design.
Below is an example implementation using file tree: Each .ts file represents a callable tool exposed by an MCP server. The agent can:
📁 mcp-tools/
├── 📁 data/
│ ├── 📄 fetch_csv.ts
│ ├── 📄 clean_dataframe.ts
│ └── 📄 summarize_table.ts
├── 📁 web/
│ ├── 📄 search_web.ts
│ ├── 📄 extract_links.ts
│ └── 📄 scrape_content.ts
├── 📁 finance/
│ ├── 📄 get_stock_price.ts
│ ├── 📄 convert_currency.ts
│ └── 📄 forecast_trends.ts
├── 📁 utils/
│ ├── 📄 log_event.ts
│ ├── 📄 format_date.ts
│ └── 📄 generate_uuid.ts
This means:
Now, let’s go over the three points above for a clearer understanding. At the end, I’ll also provide a full example scenario to reinforce the concepts.
Agents Write Code to Call Tools: The agent doesn’t need to load the full schema of getSheet into the model context. It simply writes code that calls the tool directly. This is more efficient and flexible.
Example: Instead of using declarative tool calls inside the model context, the agent writes executable TypeScript code:
import * as gdrive from './servers/google-drive';
import * as salesforce from './servers/salesforce';
const sheetData = await gdrive.getSheet({ sheetId: 'orders-2025' });
Tool Definitions Are Discovered and Loaded on Demand: Instead of loading every tool’s full schema (which could be thousands of tokens), the agent searches for tools by name or description. It only loads the full definition if needed. This keeps the context lean and fast.
Example: The agent uses a search_tools function to find the right tool:
const tools = await search_tools({ query: 'get sheet data', detailLevel: 'name-only' });
Intermediate Results Are Processed in Code, Not Passed Through the Model: The full sheet might contain 10,000 rows. Instead of passing all that into the model (which would explode the token count), the agent filters it in code and only returns the top 5 pending orders. The model sees just what it needs.
Example: After fetching the sheet, the agent filters the data before returning anything to the model:
const pendingOrders = sheetData.filter(row => row.Status === 'pending');
console.log(pendingOrders.slice(0, 5));
What It Means:
This design allows agents to write and execute code that interacts with MCP servers dynamically.
Goal: Find pending orders in a Google Sheet and update Salesforce.
What’s happening:
import * as gdrive from './servers/google-drive';
import * as salesforce from './servers/salesforce';
const allRows = await gdrive.getSheet({ sheetId: 'orders-2025' });
const pending = allRows.filter(row => row.Status === 'pending');
await salesforce.updateRecord({
objectType: 'Order',
recordId: pending[0].id,
data: { status: 'follow-up initiated' }
});
While this new approach offers significant efficiency and privacy gains, it also introduces operational and architectural considerations that teams must carefully manage.
Code Execution with MCP represents a transformative leap in how AI agents interact with tools; shifting from context-heavy, declarative calls to lightweight, executable logic that’s modular, secure, and efficient. By offloading computation and data handling to code, agents gain the ability to scale, preserve privacy, and execute complex workflows with precision.
This approach not only reduces token usage and latency but also unlocks new patterns like reusable skills, persistent state, and dynamic tool discovery. For developers and architects building next-generation agents, embracing this model is key to delivering faster, smarter, and more maintainable AI systems.
If you’re reading this to kick off your first AI, Data project, streamline your current workflow, or upskill your team for what’s next. Tech-Insight-Group LLC is here to help. We specialize in turning AI potential into practical impact through expert consulting and hands-on training. Visit our services page to explore how we can support your journey from strategy to execution. Let’s build something extraordinary together.
Thank you for reading Code Execution with MCP: A Leaner, Smarter Way to Build AI Agents. If you found this article helpful, feel free to like, share, or leave a comment; we’d love to hear your thoughts.
Kudos to our entire team for their dedication, and a special shoutout to Jean Joseph, our Principal Data & AI Architect, whose vision and technical leadership made this work possible.
Here is the original public announcement from Anthropic: Code execution with MCP: building more efficient AI agents \ Anthropic