|
| 1 | +--- |
| 2 | +title: Connect MCP Servers with bub-mcp |
| 3 | +description: Install the bub-mcp plugin, register a time MCP server, and call its tools from a Bub turn. |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +This tutorial wires a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server into Bub through the `bub-mcp` plugin. MCP servers expose external capabilities — APIs, local tools, data sources — that Bub can call as tools during a turn. |
| 9 | + |
| 10 | +By the end you will have the official `mcp-server-time` registered with Bub and verified as connected. From there, swapping in any other stdio, HTTP, or SSE server is a one-line change. |
| 11 | + |
| 12 | +## Before you begin |
| 13 | + |
| 14 | +You need: |
| 15 | + |
| 16 | +- Bub installed and runnable with `uv run bub --help`. |
| 17 | +- [`uv`](https://docs.astral.sh/uv/) on `PATH` so `uvx mcp-server-time` can launch the [time MCP server](https://pypi.org/project/mcp-server-time/) on demand. |
| 18 | +- A working model provider if you want to call the MCP tool from a real turn (see the final section). |
| 19 | + |
| 20 | +## 1. Install bub-mcp |
| 21 | + |
| 22 | +`bub-mcp` lives in [`bubbuild/bub-contrib`](https://github.com/bubbuild/bub-contrib) and is not on PyPI. Use `bub install` — it resolves bare names against bub-contrib when given an `@<ref>` suffix: |
| 23 | + |
| 24 | +```bash |
| 25 | +bub install bub-mcp@main |
| 26 | +``` |
| 27 | + |
| 28 | +This requires Bub to be running inside a virtualenv (see [`bub install`](/docs/reference/cli/#bub-install)); activate it first if needed. |
| 29 | + |
| 30 | +Verify the plugin loaded: |
| 31 | + |
| 32 | +```bash |
| 33 | +uv run bub hooks |
| 34 | +``` |
| 35 | + |
| 36 | +You should see `mcp` listed alongside `builtin`: |
| 37 | + |
| 38 | +```text |
| 39 | +load_state: builtin, mcp |
| 40 | +provide_channels: builtin, mcp |
| 41 | +register_cli_commands: builtin, mcp |
| 42 | +``` |
| 43 | + |
| 44 | +If `mcp` is missing, the plugin landed in a different environment than the one `uv run bub` resolves. |
| 45 | + |
| 46 | +## 2. Register the time server |
| 47 | + |
| 48 | +`bub-mcp` reads server definitions from `~/.bub/mcp.json` (or `$BUB_HOME/mcp.json` when `BUB_HOME` is set). Create the file with one entry that launches `mcp-server-time` over stdio: |
| 49 | + |
| 50 | +```bash |
| 51 | +mkdir -p ~/.bub |
| 52 | +cat > ~/.bub/mcp.json <<'EOF' |
| 53 | +{ |
| 54 | + "mcpServers": { |
| 55 | + "time": { |
| 56 | + "command": "uvx", |
| 57 | + "args": ["mcp-server-time"] |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +EOF |
| 62 | +``` |
| 63 | + |
| 64 | +For stdio servers, `command` is required; `args` and `env` are optional. The presence of `command` selects stdio — there is no `transport` field on stdio entries. |
| 65 | + |
| 66 | +## 3. Verify the server is connected |
| 67 | + |
| 68 | +```bash |
| 69 | +uv run bub mcp list |
| 70 | +``` |
| 71 | + |
| 72 | +Expected output: |
| 73 | + |
| 74 | +```text |
| 75 | +🔌 MCP Tools |
| 76 | +- time |
| 77 | + Status: Connected |
| 78 | + Tools: mcp.time_get_current_time, mcp.time_convert_time |
| 79 | +``` |
| 80 | + |
| 81 | +`Status: Connected` means `bub-mcp` started the child process, completed the MCP handshake, and discovered the server's tools. Each remote tool is exposed to Bub under the prefix `mcp.<server>_<tool>`. |
| 82 | + |
| 83 | +If you see `Status: Disconnected`, run the launch command directly to debug it: |
| 84 | + |
| 85 | +```bash |
| 86 | +uvx mcp-server-time |
| 87 | +``` |
| 88 | + |
| 89 | +The process should start without exiting. Press `Ctrl-C` to stop it, fix the underlying issue, then re-run `bub mcp list`. |
| 90 | + |
| 91 | +## 4. Use the MCP tool from a running turn |
| 92 | + |
| 93 | +`bub mcp list` is enough to confirm the integration. Calling the tool from a real turn requires Bub's **channel runtime**, which only `bub gateway` starts: |
| 94 | + |
| 95 | +- `bub run` and `bub chat` do **not** start any `Channel`. The `mcp.lifecycle` channel that owns the MCP servers never boots, so MCP tools are not exposed to the model in those commands. |
| 96 | +- `bub gateway` starts every channel returned by the `provide_channels` hook (subject to `--enable-channel` / `BUB_ENABLED_CHANNELS`). With `mcp.lifecycle` enabled, the channel boots in the background, registers each remote tool into the global tool registry as `mcp.<server>_<tool>`, and from then on the model can call them. |
| 97 | + |
| 98 | +Run the gateway with both the input channel and the MCP lifecycle channel enabled: |
| 99 | + |
| 100 | +```bash |
| 101 | +uv run bub gateway --enable-channel cli --enable-channel mcp.lifecycle |
| 102 | +``` |
| 103 | + |
| 104 | +Wait a few seconds after `channel.manager started listening` so the MCP bootstrap can complete, then ask Bub a question that needs the time server (for example, `What time is it right now in UTC?`). The model should call `mcp.time_get_current_time` and include the result in its reply. |
| 105 | + |
| 106 | +If the model answers without calling the MCP tool, the bootstrap had not finished yet when the turn started — wait longer or send a warm-up message first. The bootstrap is asynchronous (`asyncio.create_task`), so it does not block channel startup, but it also does not block the first turn. |
| 107 | + |
| 108 | +For long-running deployments, see [Deploy](/docs/operate/deploy/) — the same gateway invocation is what runs in the container image. |
| 109 | + |
| 110 | +## Add other server types |
| 111 | + |
| 112 | +Edit `~/.bub/mcp.json` to add more entries under `mcpServers`. Each transport has its own shape. |
| 113 | + |
| 114 | +**HTTP** — `url` plus `transport: "http"`, with optional `headers`: |
| 115 | + |
| 116 | +```json |
| 117 | +{ |
| 118 | + "weather": { |
| 119 | + "url": "https://weather.example.com/mcp", |
| 120 | + "transport": "http" |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +**SSE** — `url` plus `transport: "sse"`, with optional `headers`: |
| 126 | + |
| 127 | +```json |
| 128 | +{ |
| 129 | + "events": { |
| 130 | + "url": "https://events.example.com/mcp", |
| 131 | + "transport": "sse", |
| 132 | + "headers": { "Authorization": "Bearer token" } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +**Another stdio server** — for example, the Node [`@modelcontextprotocol/server-filesystem`](https://www.npmjs.com/package/@modelcontextprotocol/server-filesystem) over `npx`. Add allowed directories as positional `args`, and pass credentials through `env`: |
| 138 | + |
| 139 | +```json |
| 140 | +{ |
| 141 | + "filesystem": { |
| 142 | + "command": "npx", |
| 143 | + "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"] |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +After saving, run `bub mcp list` to confirm each new server connects. |
| 149 | + |
| 150 | +### CLI alternatives |
| 151 | + |
| 152 | +If you prefer the CLI over editing JSON, `bub mcp add` writes the same entries: |
| 153 | + |
| 154 | +```bash |
| 155 | +# stdio |
| 156 | +uv run bub mcp add --transport stdio time -- uvx mcp-server-time |
| 157 | +uv run bub mcp add --transport stdio --env API_KEY=secret example -- node ./my-server.js |
| 158 | + |
| 159 | +# http / sse |
| 160 | +uv run bub mcp add --transport http weather https://weather.example.com/mcp |
| 161 | +uv run bub mcp add --transport sse --header "Authorization: Bearer token" \ |
| 162 | + events https://events.example.com/mcp |
| 163 | + |
| 164 | +# remove |
| 165 | +uv run bub mcp remove time |
| 166 | +``` |
| 167 | + |
| 168 | +`--env` is only allowed with `--transport stdio`; `--header` is only allowed with `--transport http` or `--transport sse`. |
| 169 | + |
| 170 | +## Troubleshooting |
| 171 | + |
| 172 | +| Symptom | Check | |
| 173 | +|---|---| |
| 174 | +| `mcp` does not appear in `bub hooks` | The plugin was installed in a different environment than `uv run bub` resolves. Re-install into the active Bub venv. | |
| 175 | +| `bub mcp list` reports `Status: Disconnected` | Run the configured `command` (or open the `url`) outside Bub and confirm it starts cleanly; the error column shows the underlying cause. | |
| 176 | +| `bub mcp add` prints `CancelledError` after `Added MCP server …` | Cosmetic only — the entry is written. Use `bub mcp list` to verify, or edit `mcp.json` by hand. | |
| 177 | +| Tool never called during a turn | Confirm `bub mcp list` shows `Status: Connected` and lists the expected tool, then ask a question that clearly maps to that tool. | |
| 178 | +| Permission denied on `mcp.json` | Verify `~/.bub/` is writable, or set `BUB_HOME` to a directory you own. | |
| 179 | + |
| 180 | +## Next steps |
| 181 | + |
| 182 | +- [Build plugins](/docs/build/plugins/) — write your own Bub plugins. |
| 183 | +- [Configure](/docs/operate/configure/) — Bub's config layer and environment variables. |
| 184 | +- [bub-mcp source](https://github.com/bubbuild/bub-contrib/tree/main/packages/bub-mcp) — the plugin's full source and advanced options. |
0 commit comments