Skip to content

Commit bc703e1

Browse files
committed
updated cookbook
1 parent a4c9c64 commit bc703e1

File tree

11 files changed

+134
-123
lines changed

11 files changed

+134
-123
lines changed
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# Environment variables for Browserbase MCP example
22

3-
E2B_API_KEY=your_e2b_api_key
4-
BROWSERBASE_API_KEY=your_browserbase_api_key
5-
BROWSERBASE_PROJECT_ID=your_browserbase_project_id
6-
GEMINI_API_KEY=your_gemini_api_key
7-
OPENAI_API_KEY=your_openai_api_key
3+
# E2B API key - get from https://e2b.dev/docs/api-key
4+
E2B_API_KEY=
5+
6+
# Browserbase API key - get from https://browserbase.com
7+
BROWSERBASE_API_KEY=
8+
9+
# Browserbase project ID - get from your Browserbase dashboard
10+
BROWSERBASE_PROJECT_ID=
11+
12+
# Gemini API key for Browserbase - get from https://ai.google.dev/
13+
GEMINI_API_KEY=
14+
15+
# OpenAI API key for agents - get from https://platform.openai.com/api-keys
16+
OPENAI_API_KEY=
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Environment variables for Claude Code MCP example
1+
# E2B API Key
2+
# Get your API key from: https://e2b.dev/docs/getting-started/api-key
3+
E2B_API_KEY=e2b_your_api_key_here
24

3-
E2B_API_KEY=your_e2b_api_key
4-
ANTHROPIC_API_KEY=your_anthropic_api_key
5+
# Anthropic API Key
6+
# Get your API key from: https://console.anthropic.com/
7+
ANTHROPIC_API_KEY=sk-ant-your_anthropic_api_key_here
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Environment variables for MCP Client example
2-
3-
E2B_API_KEY=your_e2b_api_key
1+
# E2B API Key
2+
# Get your API key from: https://e2b.dev/docs/getting-started/api-key
3+
E2B_API_KEY=e2b_your_api_key_here
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Environment variables for Custom Server MCP example
2-
3-
E2B_API_KEY=your_e2b_api_key
1+
# E2B API Key
2+
# Get your API key from: https://e2b.dev/docs/getting-started/api-key
3+
E2B_API_KEY=e2b_your_api_key_here

examples/mcp-custom-server-js/index.ts

Lines changed: 66 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,89 @@ import Sandbox from 'e2b';
33
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
44
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
55

6-
async function runCustomServerExample() {
6+
async function run() {
77
console.log('Creating E2B sandbox with custom filesystem MCP server...');
8-
9-
let sandbox: Sandbox | null = null;
10-
11-
try {
12-
// Create E2B sandbox with custom MCP server from GitHub
13-
sandbox = await Sandbox.create({
14-
mcp: {
15-
'github/modelcontextprotocol/servers': {
16-
installCmd: 'npm install',
17-
runCmd: 'npx -y @modelcontextprotocol/server-filesystem /root',
18-
},
8+
9+
// Create E2B sandbox with custom MCP server from GitHub
10+
const sandbox = await Sandbox.create({
11+
mcp: {
12+
'github/modelcontextprotocol/servers': {
13+
installCmd: 'npm install',
14+
runCmd: 'npx -y @modelcontextprotocol/server-filesystem /root',
1915
},
20-
timeoutMs: 600_000, // 10 minutes
21-
});
16+
},
17+
timeoutMs: 600_000, // 10 minutes
18+
});
2219

23-
console.log('Sandbox created successfully');
24-
console.log(`MCP URL: ${sandbox.getMcpUrl()}`);
20+
console.log('Sandbox created successfully');
21+
console.log(`MCP URL: ${sandbox.getMcpUrl()}`);
2522

26-
// Create MCP client
27-
const client = new Client({
28-
name: 'e2b-custom-server-client',
29-
version: '1.0.0'
30-
});
23+
// Create MCP client
24+
const client = new Client({
25+
name: 'e2b-custom-server-client',
26+
version: '1.0.0'
27+
});
3128

32-
// Set up transport with authentication
33-
const transport = new StreamableHTTPClientTransport(
34-
new URL(sandbox.getMcpUrl()),
35-
{
36-
requestInit: {
37-
headers: {
38-
'Authorization': `Bearer ${await sandbox.getMcpToken()}`
39-
}
29+
// Set up transport with authentication
30+
const transport = new StreamableHTTPClientTransport(
31+
new URL(sandbox.getMcpUrl()),
32+
{
33+
requestInit: {
34+
headers: {
35+
'Authorization': `Bearer ${await sandbox.getMcpToken()}`
4036
}
4137
}
42-
);
43-
44-
console.log('Connecting to custom MCP server...');
45-
await client.connect(transport);
46-
console.log('Connected to custom MCP server successfully');
47-
48-
// List available tools
49-
console.log('\nAvailable tools from custom filesystem MCP server:');
50-
const tools = await client.listTools();
51-
52-
if (tools.tools.length === 0) {
53-
console.log('No tools available from custom MCP server');
54-
console.log(`\nTotal tools available: ${tools.tools.length}`);
55-
return;
5638
}
39+
);
5740

58-
tools.tools.forEach((tool, index) => {
59-
console.log(`${index + 1}. ${tool.name}`);
60-
if (tool.description) {
61-
console.log(` Description: ${tool.description}`);
62-
}
63-
});
41+
console.log('Connecting to custom MCP server...');
42+
await client.connect(transport);
43+
console.log('Connected to custom MCP server successfully');
6444

45+
// List available tools
46+
console.log('\nAvailable tools from custom filesystem MCP server:');
47+
const tools = await client.listTools();
48+
49+
if (tools.tools.length === 0) {
50+
console.log('No tools available from custom MCP server');
6551
console.log(`\nTotal tools available: ${tools.tools.length}`);
52+
return;
53+
}
6654

67-
// Example: Use the filesystem tools if available
68-
console.log('\nTesting filesystem operations...');
69-
70-
// Try to list files in the root directory
71-
const listFilesTool = tools.tools.find(tool => tool.name === 'list_directory');
72-
if (!listFilesTool) {
73-
console.log('list_directory tool not available');
74-
return;
55+
tools.tools.forEach((tool, index) => {
56+
console.log(`${index + 1}. ${tool.name}`);
57+
if (tool.description) {
58+
console.log(` Description: ${tool.description}`);
7559
}
60+
});
7661

77-
console.log('Listing files in root directory...');
78-
try {
79-
const result = await client.callTool({
80-
name: 'list_directory',
81-
arguments: { path: '/' }
82-
});
83-
console.log('Directory contents:', result.content);
84-
} catch (error) {
85-
console.log('Could not list directory:', error);
86-
}
62+
console.log(`\nTotal tools available: ${tools.tools.length}`);
8763

88-
} catch (error) {
89-
console.error('Error occurred:', error);
90-
throw error;
91-
} finally {
92-
// Cleanup
93-
if (sandbox) {
94-
console.log('\nCleaning up sandbox...');
95-
await sandbox.kill();
96-
console.log('Sandbox closed successfully');
97-
}
64+
// Example: Use the filesystem tools if available
65+
console.log('\nTesting filesystem operations...');
66+
67+
// Try to list files in the root directory
68+
const listFilesTool = tools.tools.find(tool => tool.name === 'list_directory');
69+
if (!listFilesTool) {
70+
console.log('list_directory tool not available');
71+
return;
9872
}
73+
74+
console.log('Listing files in root directory...');
75+
const result = await client.callTool({
76+
name: 'list_directory',
77+
arguments: { path: '/' }
78+
});
79+
console.log('Directory contents:', result.content);
80+
81+
// Cleanup
82+
console.log('\nCleaning up sandbox...');
83+
await sandbox.kill();
84+
console.log('Sandbox closed successfully');
9985
}
10086

101-
// Run the custom server example
102-
runCustomServerExample().catch((error) => {
103-
console.error('Failed to run custom server example:', error);
87+
// Run the example
88+
run().catch((error) => {
89+
console.error('Failed to run example:', error);
10490
process.exit(1);
10591
});

examples/mcp-custom-template-js/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Creates a custom E2B template with pre-installed MCP servers for faster startup.
1212
BROWSERBASE_PROJECT_ID=your_browserbase_project_id
1313
GEMINI_API_KEY=your_gemini_api_key
1414
```
15-
3. Run: `npm run start`
15+
3. Build the template: `npm run build`
16+
4. Run: `npm run start`
1617

1718
## What it does
1819

1920
- Creates custom E2B template with pre-installed MCP servers
20-
- Builds template with Browserbase and E2B MCP servers cached
21+
- Builds template with Browserbase and E2B MCP servers
2122
- Creates sandbox from custom template
22-
- Lists available tools from pre-installed servers
23+
- Lists available tools from pre-installed servers
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Template } from "e2b";
2-
import { template, alias } from "./template";
1+
import { Template } from 'e2b';
2+
import { template, alias } from './template';
33

44
async function buildTemplate() {
55
console.log('Building custom E2B template with pre-installed MCP servers...');
6-
6+
77
await Template.build(template, {
88
alias,
99
cpuCount: 8,
@@ -15,4 +15,8 @@ async function buildTemplate() {
1515
console.log(`Template alias: ${alias}`);
1616
}
1717

18-
buildTemplate();
18+
buildTemplate().catch((error) => {
19+
console.error('Failed to run Claude Code example:', error);
20+
process.exit(1);
21+
});
22+
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
# Environment variables for Custom Template MCP example
1+
# E2B API Key
2+
# Get your API key from: https://e2b.dev/docs/getting-started/api-key
3+
E2B_API_KEY=e2b_your_api_key_here
24

3-
E2B_API_KEY=your_e2b_api_key
4-
BROWSERBASE_API_KEY=your_browserbase_api_key
5-
BROWSERBASE_PROJECT_ID=your_browserbase_project_id
6-
GEMINI_API_KEY=your_gemini_api_key
5+
# Browserbase API Key
6+
# Get your API key from: https://browserbase.com/
7+
BROWSERBASE_API_KEY=bb_your_browserbase_api_key_here
8+
9+
# Gemini API Key
10+
# Get your API key from: https://ai.google.dev/
11+
GEMINI_API_KEY=your_gemini_api_key_here
12+
13+
# Browserbase Project ID
14+
# Get your project ID from: https://browserbase.com/
15+
BROWSERBASE_PROJECT_ID=your_project_id_here

examples/mcp-custom-template-js/sandbox.ts renamed to examples/mcp-custom-template-js/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import 'dotenv/config';
2-
import { Sandbox } from "e2b";
2+
import Sandbox from 'e2b';
33
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
44
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
5-
import { alias } from "./template";
5+
import { alias } from './template';
66

7-
async function runSandboxExample() {
8-
console.log('Creating sandbox from custom template and testing MCP servers...');
9-
10-
7+
async function runSandboxExample() {
118
// Create sandbox from custom template
9+
// Make sure the template is built first by running `npm run build`
1210
console.log('Creating sandbox from custom template...');
1311
const sandbox = await Sandbox.create(alias, {
1412
mcp: {
@@ -69,11 +67,12 @@ async function runSandboxExample() {
6967
console.log(`\nTotal tools available: ${tools.tools.length}`);
7068

7169
// Cleanup
72-
if (sandbox) {
73-
console.log('\nCleaning up sandbox...');
74-
await sandbox.kill();
75-
console.log('Sandbox closed successfully');
76-
}
70+
console.log('\nCleaning up sandbox...');
71+
await sandbox.kill();
72+
console.log('Sandbox closed successfully');
7773
}
7874

79-
runSandboxExample();
75+
runSandboxExample().catch((error) => {
76+
console.error('Failed to run example:', error);
77+
process.exit(1);
78+
});

examples/mcp-custom-template-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.ts",
66
"scripts": {
77
"build": "tsx build.ts",
8-
"start": "tsx sandbox.ts",
8+
"start": "tsx index.ts",
99
"test": "echo \"Error: no test specified\" && exit 1"
1010
},
1111
"keywords": ["e2b", "mcp", "template", "custom", "browserbase"],

0 commit comments

Comments
 (0)