@@ -3,103 +3,89 @@ import Sandbox from 'e2b';
33import { Client } from '@modelcontextprotocol/sdk/client/index.js' ;
44import { 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} ) ;
0 commit comments