|
| 1 | +# MCP-Use TypeScript Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating how to use the mcp-use library with various MCP servers. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. **Node.js**: Ensure you have Node.js 22+ installed |
| 8 | +2. **Environment Variables**: Create a `.env` file in the project root with your API keys: |
| 9 | + ```bash |
| 10 | + ANTHROPIC_API_KEY=your_anthropic_key |
| 11 | + OPENAI_API_KEY=your_openai_key |
| 12 | + E2B_API_KEY=your_e2b_key # Only for sandbox example |
| 13 | + ``` |
| 14 | + |
| 15 | +## Running Examples |
| 16 | + |
| 17 | +First, build the library: |
| 18 | +```bash |
| 19 | +npm run build |
| 20 | +``` |
| 21 | + |
| 22 | +Then run any example using Node.js: |
| 23 | +```bash |
| 24 | +node dist/examples/example_name.js |
| 25 | +``` |
| 26 | + |
| 27 | +Or use the npm scripts: |
| 28 | +```bash |
| 29 | +npm run example:airbnb |
| 30 | +npm run example:browser |
| 31 | +npm run example:chat |
| 32 | +# ... etc |
| 33 | +``` |
| 34 | + |
| 35 | +## Available Examples |
| 36 | + |
| 37 | +### 1. Airbnb Search (`airbnb_use.ts`) |
| 38 | +Search for accommodations using the Airbnb MCP server. |
| 39 | +```bash |
| 40 | +npm run example:airbnb |
| 41 | +``` |
| 42 | + |
| 43 | +### 2. Browser Automation (`browser_use.ts`) |
| 44 | +Control a browser using Playwright MCP server. |
| 45 | +```bash |
| 46 | +npm run example:browser |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Interactive Chat (`chat_example.ts`) |
| 50 | +Interactive chat session with conversation memory. |
| 51 | +```bash |
| 52 | +npm run example:chat |
| 53 | +``` |
| 54 | + |
| 55 | +### 4. File System Operations (`filesystem_use.ts`) |
| 56 | +Access and manipulate files using the filesystem MCP server. |
| 57 | +```bash |
| 58 | +# First, edit the example to set your directory path |
| 59 | +npm run example:filesystem |
| 60 | +``` |
| 61 | + |
| 62 | +### 5. HTTP Server Connection (`http_example.ts`) |
| 63 | +Connect to an MCP server via HTTP. |
| 64 | +```bash |
| 65 | +# First, start the Playwright server in another terminal: |
| 66 | +npx @playwright/mcp@latest --port 8931 |
| 67 | + |
| 68 | +# Then run the example: |
| 69 | +npm run example:http |
| 70 | +``` |
| 71 | + |
| 72 | +### 6. MCP Everything Test (`mcp_everything.ts`) |
| 73 | +Test various MCP functionalities. |
| 74 | +```bash |
| 75 | +npm run example:everything |
| 76 | +``` |
| 77 | + |
| 78 | +### 7. Multiple Servers (`multi_server_example.ts`) |
| 79 | +Use multiple MCP servers in a single session. |
| 80 | +```bash |
| 81 | +# First, edit the example to set your directory path |
| 82 | +npm run example:multi |
| 83 | +``` |
| 84 | + |
| 85 | +### 8. Sandboxed Environment (`sandbox_everything.ts`) |
| 86 | +Run MCP servers in an E2B sandbox (requires E2B_API_KEY). |
| 87 | +```bash |
| 88 | +npm run example:sandbox |
| 89 | +``` |
| 90 | + |
| 91 | +### 9. OAuth Authentication (`simple_oauth_example.ts`) |
| 92 | +OAuth flow example with Linear. |
| 93 | +```bash |
| 94 | +# First, register your app with Linear and update the client_id |
| 95 | +npm run example:oauth |
| 96 | +``` |
| 97 | + |
| 98 | +### 10. Blender Integration (`blender_use.ts`) |
| 99 | +Control Blender 3D through MCP. |
| 100 | +```bash |
| 101 | +# First, install and enable the Blender MCP addon |
| 102 | +npm run example:blender |
| 103 | +``` |
| 104 | + |
| 105 | +## Configuration Files |
| 106 | + |
| 107 | +Some examples use JSON configuration files: |
| 108 | +- `airbnb_mcp.json` - Airbnb server configuration |
| 109 | +- `browser_mcp.json` - Browser server configuration |
| 110 | + |
| 111 | +## Environment Variables |
| 112 | + |
| 113 | +Different examples require different API keys: |
| 114 | +- **ANTHROPIC_API_KEY**: For examples using Claude (airbnb, multi_server, blender) |
| 115 | +- **OPENAI_API_KEY**: For examples using GPT (browser, chat, filesystem, http, everything) |
| 116 | +- **E2B_API_KEY**: Only for the sandbox example |
| 117 | + |
| 118 | +## Troubleshooting |
| 119 | + |
| 120 | +1. **Module not found**: Make sure to build the project first with `npm run build` |
| 121 | +2. **API key errors**: Check your `.env` file has the required keys |
| 122 | +3. **Server connection failed**: Some examples require external servers to be running |
| 123 | +4. **Permission errors**: Some examples may need specific permissions (e.g., filesystem access) |
| 124 | + |
| 125 | +## Writing Your Own Examples |
| 126 | + |
| 127 | +To create a new example: |
| 128 | + |
| 129 | +1. Import the necessary modules: |
| 130 | + ```typescript |
| 131 | + import { MCPAgent, MCPClient } from '../index.js' |
| 132 | + import { ChatOpenAI } from '@langchain/openai' |
| 133 | + ``` |
| 134 | + |
| 135 | +2. Configure your MCP server: |
| 136 | + ```typescript |
| 137 | + const config = { |
| 138 | + mcpServers: { |
| 139 | + yourServer: { |
| 140 | + command: 'npx', |
| 141 | + args: ['your-mcp-server'] |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + ``` |
| 146 | + |
| 147 | +3. Create client, LLM, and agent: |
| 148 | + ```typescript |
| 149 | + const client = MCPClient.fromDict(config) |
| 150 | + const llm = new ChatOpenAI({ model: 'gpt-4o' }) |
| 151 | + const agent = new MCPAgent({ llm, client, maxSteps: 30 }) |
| 152 | + ``` |
| 153 | + |
| 154 | +4. Run your queries: |
| 155 | + ```typescript |
| 156 | + const result = await agent.run('Your prompt here', { maxSteps: 30 }) |
| 157 | + ``` |
| 158 | + |
| 159 | +## Contributing |
| 160 | + |
| 161 | +Feel free to add more examples! Make sure to: |
| 162 | +1. Follow the existing code style |
| 163 | +2. Add appropriate documentation |
| 164 | +3. Update this README with your example |
| 165 | +4. Add a corresponding npm script in package.json |
0 commit comments