A demonstration of using Model Context Protocol (MCP) servers with E2B sandboxes and Mastra agents. This project showcases how to run MCP servers in secure E2B sandboxes and integrate them with AI agents.
This demo creates an AI agent that can access MCP servers (like Context7 for documentation) running inside an E2B sandbox. The agent can then use the tools provided by these MCP servers to answer questions.
Before getting started, you'll need:
- Bun installed (or Node.js)
- An E2B API key
- An OpenAI API key
# Install dependencies
bun installCreate a .env file in the project root:
# E2B API key for sandbox creation
E2B_API_KEY=your_e2b_api_key
# OpenAI API key for the agent
OPENAI_API_KEY=your_openai_api_keybun run index.tsThe demo will:
- Create an E2B sandbox with the Context7 MCP server
- Initialize a Mastra agent with access to the MCP tools
- Ask the agent "How to create an E2B Sandbox?"
- Display the agent's response
┌─────────────┐
│ Mastra Agent│
└──────┬──────┘
│
▼
┌─────────────┐
│ MCP Client │
└──────┬──────┘
│
▼
┌─────────────┐
│ E2B Sandbox │
│ ┌────────┐ │
│ │Context7│ │
│ │ MCP │ │
│ └────────┘ │
└─────────────┘
- E2B Sandbox: Provides a secure, isolated environment for MCP servers
- MCP Client: Connects to the MCP servers through the E2B gateway
- Mastra Agent: Uses the MCP tools to answer questions
index.ts- Main entry point, creates the agent and runs a querymcp.ts- Sets up the E2B sandbox with MCP servers and creates the MCP client
Edit the servers configuration in index.ts:
const servers: McpServer = {
context7: {},
// Add more MCP servers here
// Example:
// filesystem: {
// args: ["/path/to/directory"]
// }
};Modify the agent configuration in index.ts:
const agent = new Agent({
name: "DocsAgent",
instructions: "Your custom instructions here",
model: "openai/gpt-4o", // or another model
tools: await E2BMCPClient.getTools(),
});Update the query in index.ts:
const result = await agent.generate("Your question here");