|
| 1 | +--- |
| 2 | +title: 'MCP Servers' |
| 3 | +section: 'agents' |
| 4 | +tags: ['agents', 'mcp', 'integrations', 'model-context-protocol'] |
| 5 | +order: 3 |
| 6 | +--- |
| 7 | + |
| 8 | +# MCP Servers |
| 9 | + |
| 10 | +The Model Context Protocol (MCP) is an open standard that lets you connect AI agents to external tools and data sources. Codebuff agents can use MCP servers to access APIs, databases, and other services. |
| 11 | + |
| 12 | +## How It Works |
| 13 | + |
| 14 | +To use an MCP server, create an agent in your `.agents/` directory and configure the `mcpServers` field. The MCP server will be started automatically when the agent runs, and its tools will be available to the agent. |
| 15 | + |
| 16 | +## Example: Notion Integration |
| 17 | + |
| 18 | +Here's a complete example that connects to Notion using the official Notion MCP server: |
| 19 | + |
| 20 | +**.agents/notion-agent.ts** |
| 21 | + |
| 22 | +```typescript |
| 23 | +import type { AgentDefinition } from './types/agent-definition' |
| 24 | + |
| 25 | +const definition: AgentDefinition = { |
| 26 | + id: 'notion-query-agent', |
| 27 | + displayName: 'Notion Query Agent', |
| 28 | + model: 'anthropic/claude-sonnet-4.5', |
| 29 | + |
| 30 | + spawnerPrompt: |
| 31 | + 'Expert at querying Notion databases and pages to find information and answer questions about content stored in Notion workspaces.', |
| 32 | + |
| 33 | + inputSchema: { |
| 34 | + prompt: { |
| 35 | + type: 'string', |
| 36 | + description: |
| 37 | + 'A question or request about information stored in your Notion workspace', |
| 38 | + }, |
| 39 | + }, |
| 40 | + |
| 41 | + outputMode: 'last_message', |
| 42 | + includeMessageHistory: false, |
| 43 | + |
| 44 | + mcpServers: { |
| 45 | + notionApi: { |
| 46 | + command: 'npx', |
| 47 | + args: ['-y', '@notionhq/notion-mcp-server'], |
| 48 | + env: { |
| 49 | + NOTION_TOKEN: '$NOTION_TOKEN', |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + |
| 54 | + systemPrompt: `You are a Notion expert who helps users find and retrieve information from their Notion workspace. You can search across pages and databases, read specific pages, and query databases with filters.`, |
| 55 | + |
| 56 | + instructionsPrompt: `Instructions: |
| 57 | +1. Use the Notion tools to search for relevant information based on the user's question. Start with a broad search. |
| 58 | +2. If you find relevant pages or databases, read them in detail or query them with appropriate filters |
| 59 | +3. Provide a comprehensive answer based on the information found in Notion. |
| 60 | +`, |
| 61 | +} |
| 62 | + |
| 63 | +export default definition |
| 64 | +``` |
| 65 | + |
| 66 | +Steps: |
| 67 | + |
| 68 | +1. Run `/init` within Codebuff to set up your `.agents` directory. |
| 69 | +2. Save this file to `.agents/notion-agent.ts` in your project. |
| 70 | +3. Get your [Notion key](https://developers.notion.com/docs/get-started-with-mcp) and set it as an environment variable. |
| 71 | +4. Start Codebuff and ask it to use your new Notion agent! |
| 72 | + |
| 73 | +Use similar steps to create new agents with other mcp tools! |
| 74 | + |
| 75 | +## Configuration Reference |
| 76 | + |
| 77 | +### `mcpServers` (object) |
| 78 | + |
| 79 | +A map of MCP server configurations. Each key is a name for the server (used for identification), and the value is the server configuration. |
| 80 | + |
| 81 | +There are two types of MCP server configurations: |
| 82 | + |
| 83 | +### Stdio (Local Process) |
| 84 | + |
| 85 | +Runs an MCP server as a local process that communicates via stdin/stdout: |
| 86 | + |
| 87 | +```typescript |
| 88 | +mcpServers: { |
| 89 | + serverName: { |
| 90 | + type: 'stdio', // Optional, defaults to 'stdio' |
| 91 | + command: string, // Command to run the MCP server |
| 92 | + args: string[], // Arguments to pass to the command |
| 93 | + env: { // Environment variables for the server |
| 94 | + VAR_NAME: string, // Use '$VAR_NAME' to reference environment variables |
| 95 | + }, |
| 96 | + }, |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### Stdio Fields |
| 101 | + |
| 102 | +- **`type`** (`'stdio'`): Optional. Indicates a local process server (default) |
| 103 | +- **`command`** (`string`): The command to execute (e.g., `'npx'`, `'node'`, `'python'`) |
| 104 | +- **`args`** (`string[]`): Arguments passed to the command |
| 105 | +- **`env`** (`object`): Environment variables for the MCP server process |
| 106 | + |
| 107 | +### Remote (HTTP/SSE) |
| 108 | + |
| 109 | +Connects to a remote MCP server via HTTP or Server-Sent Events (SSE): |
| 110 | + |
| 111 | +```typescript |
| 112 | +mcpServers: { |
| 113 | + serverName: { |
| 114 | + type: 'http', // 'http' or 'sse' |
| 115 | + url: string, // URL of the remote MCP server |
| 116 | + params: { // Query parameters to include in requests |
| 117 | + paramName: string, |
| 118 | + }, |
| 119 | + headers: { // HTTP headers to include in requests |
| 120 | + headerName: string, // Use '$VAR_NAME' to reference environment variables |
| 121 | + }, |
| 122 | + }, |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +#### Remote Fields |
| 127 | + |
| 128 | +- **`type`** (`'http'` | `'sse'`): Required. `'http'` for standard HTTP, `'sse'` for Server-Sent Events |
| 129 | +- **`url`** (`string`): The URL of the remote MCP server |
| 130 | +- **`params`** (`object`): Query parameters to include in requests |
| 131 | +- **`headers`** (`object`): HTTP headers to include in requests (e.g., for authentication) |
| 132 | + |
| 133 | +### Environment Variables |
| 134 | + |
| 135 | +Use the `$VAR_NAME` syntax to reference environment variables from your shell. For example: |
| 136 | + |
| 137 | +```typescript |
| 138 | +env: { |
| 139 | + NOTION_TOKEN: '$NOTION_TOKEN', |
| 140 | + API_KEY: '$MY_API_KEY', |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +This reads `NOTION_TOKEN` and `MY_API_KEY` from your environment and passes them to the MCP server. |
| 145 | + |
| 146 | +**Setup:** Add your token to your shell configuration (e.g., `.bashrc`, `.zshrc`): |
| 147 | + |
| 148 | +```bash |
| 149 | +export NOTION_TOKEN="your-notion-integration-token" |
| 150 | +``` |
| 151 | + |
| 152 | +Or use a `.env` file in your project root. |
| 153 | + |
| 154 | +## Using Your MCP Agent |
| 155 | + |
| 156 | +### Spawning with `@` |
| 157 | + |
| 158 | +Reference your agent in the CLI using `@` followed by the agent's display name: |
| 159 | + |
| 160 | +``` |
| 161 | +@Notion Query Agent what meetings do I have this week? |
| 162 | +``` |
| 163 | + |
| 164 | +Codebuff will spawn your agent to handle the request. |
| 165 | + |
| 166 | +### Spawning from Other Agents |
| 167 | + |
| 168 | +Other agents can spawn your MCP-enabled agent if it's listed in their `spawnableAgents`: |
| 169 | + |
| 170 | +```typescript |
| 171 | +spawnableAgents: ['notion-query-agent'] |
| 172 | +``` |
| 173 | + |
| 174 | +## Customizing When Your Agent Is Spawned |
| 175 | + |
| 176 | +The `spawnerPrompt` field tells other agents when they should spawn your agent. Write a clear description of your agent's capabilities: |
| 177 | + |
| 178 | +```typescript |
| 179 | +spawnerPrompt: |
| 180 | + 'Expert at querying Notion databases and pages to find information and answer questions about content stored in Notion workspaces.', |
| 181 | +``` |
| 182 | + |
| 183 | +The base agent reads this description and decides whether to spawn your agent based on the user's request. Make it specific and descriptive so the base agent knows when your agent is the right choice. |
| 184 | + |
| 185 | +## More MCP Server Examples |
| 186 | + |
| 187 | +### GitHub Integration (Stdio) |
| 188 | + |
| 189 | +```typescript |
| 190 | +mcpServers: { |
| 191 | + github: { |
| 192 | + command: 'npx', |
| 193 | + args: ['-y', '@modelcontextprotocol/server-github'], |
| 194 | + env: { |
| 195 | + GITHUB_PERSONAL_ACCESS_TOKEN: '$GITHUB_TOKEN', |
| 196 | + }, |
| 197 | + }, |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +### Remote API Integration (HTTP) |
| 202 | + |
| 203 | +```typescript |
| 204 | +mcpServers: { |
| 205 | + myApi: { |
| 206 | + type: 'http', |
| 207 | + url: 'https://api.example.com/mcp', |
| 208 | + headers: { |
| 209 | + Authorization: '$API_TOKEN', |
| 210 | + }, |
| 211 | + }, |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +### Streaming Server (SSE) |
| 216 | + |
| 217 | +```typescript |
| 218 | +mcpServers: { |
| 219 | + streamingApi: { |
| 220 | + type: 'sse', |
| 221 | + url: 'https://stream.example.com/mcp/events', |
| 222 | + headers: { |
| 223 | + 'X-API-Key': '$STREAM_API_KEY', |
| 224 | + }, |
| 225 | + params: { |
| 226 | + workspace: 'default', |
| 227 | + }, |
| 228 | + }, |
| 229 | +} |
| 230 | +``` |
| 231 | + |
| 232 | +## Finding MCP Servers |
| 233 | + |
| 234 | +Browse available MCP servers at: |
| 235 | + |
| 236 | +- [MCP Server Registry](https://github.com/modelcontextprotocol/servers) - Official and community servers |
| 237 | +- [NPM](https://www.npmjs.com/search?q=mcp-server) - Search for `mcp-server` packages |
| 238 | + |
| 239 | +## Troubleshooting |
| 240 | + |
| 241 | +**Agent not connecting to MCP server:** |
| 242 | +- Verify the command and args are correct |
| 243 | +- Check that environment variables are set in your shell |
| 244 | +- Run the MCP server command manually to test it works |
| 245 | + |
| 246 | +**Environment variable not found:** |
| 247 | +- Ensure the variable is exported in your shell |
| 248 | +- Restart your terminal after adding to `.bashrc`/`.zshrc` |
| 249 | +- Check for typos in the `$VAR_NAME` reference |
| 250 | + |
| 251 | +**MCP server tools not appearing:** |
| 252 | +- The server may take a moment to start |
| 253 | +- Check the server's documentation for required setup steps |
0 commit comments