# Cliente MCP

URL: https://swb.one/es/docs/setup/mcp-client

MCP es la forma estándar en que una app usa herramientas externas. Esta guía empareja una app con SwB mediante una clave.

## Antes de empezar [#antes-de-empezar]

Usa una app compatible con MCP remoto sobre Streamable HTTP. Este método permite a las apps llamar a herramientas por la web. La app también debe admitir cabeceras de petición personalizadas para la clave. Necesitas un cliente de propietario de SwB o una terminal en el ordenador de SwB.

Para iniciar SwB en el mismo ordenador que la app, ejecuta `uvx swb-one up` en una carpeta vacía. Abre el enlace de inicio de sesión de un solo uso que aparece en tu terminal. Consulta [Instalación](/es/docs/install) para ver más opciones.

Claude, ChatGPT, Grok y los agentes de programación tienen sus propias páginas con inicio de sesión OAuth. Usa esas páginas para esas apps.

## Copiar el prompt de configuración [#copiar-el-prompt-de-configuración]

https://swb.one/prompts/mcp-client.md

````
Read the setup guide at https://swb.one/docs/setup/mcp-client.

Help me connect an MCP client that supports Streamable HTTP and custom request headers. Streamable HTTP is how the client sends tool requests to SwB.

For a new local SwB server, follow **This computer** at https://swb.one/prompts/deploy.md.

Use `http://localhost:8870` if the client runs on the SwB computer. Use the port shown at startup. Use my public HTTPS address if the client runs elsewhere.

## Owner client

Use owner access for the first client if it will set up other apps. On the SwB computer, set up a private owner client. Use the local SwB address with `/mcp` at the end. Load `SWB_KEY_OWNER` from the instance `.env` inside that client. Keep the key in its local config with file mode `0600`. Send it in the `Authorization: Bearer <key>` header only to the local address. Keep the key out of commands, logs and chat. Check that the client can call `inbox`. Give the owner client https://swb.one/prompts/coordinator.md for the remaining connections.

## Paired client

Ask me to pick a short name for the app, such as `notes-bot`. From an app with SwB owner access, call `connect(what="mcp-client", choose={"agent":"notes-bot"})` with my chosen name. If I use a terminal instead, ask me to run this command in my SwB instance folder.

```bash
swb connect mcp-client --choose agent=notes-bot
```

Use `uvx swb-one connect mcp-client --choose agent=notes-bot` if I run SwB with uvx. The result gives me a pairing code. It works once within 10 minutes.

A scope is a group of memory entries. The key gets `agent:<name>` and any scope I grant to that agent. It also gets the scope of a project named in the pairing. Read `general` if the key has access to it.

If you can edit the client settings on this computer, add an MCP server named `swb`. Use a hidden prompt if I have the pairing code. Send the code to `POST /connect` and read the response in memory. Use `mcp_url` for Streamable HTTP and `key` for the `Authorization: Bearer <key>` header. Write the key straight to the client config with file mode `0600`. Keep the code and key out of commands, logs and chat. For a local server, use the MCP address shown at startup.

If I must edit the settings myself, give me this command. Ask me to run it in a private folder on the computer that runs the client. For a different server, give me the command with its base address. Ask me to enter the pairing code at the hidden prompt.

```bash
bash -c '
  umask 077
  set -C
  read -rsp "Pairing code: " code || exit
  printf "\n"
  printf "{\"code\":\"%s\"}" "$code" |
    curl -fsS http://localhost:8870/connect -H "Content-Type: application/json" \
      --data-binary @- > .swb-pairing.json
'
```

SwB returns the key once. Ask me to open `.swb-pairing.json` in a private editor.

Ask me to add an MCP server named `swb` in the client settings. Ask me to choose Streamable HTTP. Ask me to use the `mcp_url` from the file. For a local server, ask me to use the MCP address shown at startup. Ask me to set `Authorization: Bearer <key>` with the `key` from the file. Ask me to delete the file after setup.

Have the new app call `inbox`. A successful call confirms the connection, even when the inbox is empty.

For a round trip, use an app with SwB owner access. On the SwB computer, you can use a private MCP session instead. Load `SWB_KEY_OWNER` from the instance `.env` inside the client. Send it only as a bearer token to the local SwB address. Keep the key out of commands, logs and chat.

Call `send(to="agent:notes-bot", task="SwB setup test. Reply pong.")`. Replace `notes-bot` with my chosen name. Ask the new agent to read the message with `inbox`. Ask it to answer with `reply` and the item's `handle`. From the owner session, call `wait` with the `conversation` returned by `send`. Show me the reply.

For the first shared memory check, use a scope both agents can read. Ask the new agent to call `memory_list` with the scope in `scopes`.

Ask me to paste the coordinator prompt from https://swb.one/prompts/coordinator.md into the new app. The client can subscribe to `switchboard.message.created` if it supports MCP events. It can also use `wait` or `inbox` to read messages.

For each owner-only connection, give me its matching `swb connect` command. For example, give me `swb connect slack`.
````

Pega el prompt en un cliente de propietario de SwB o en la app que quieres conectar.

## Cliente local de propietario [#cliente-local-de-propietario]

Usa un cliente privado en el ordenador de SwB para configurar otras apps. Usa la dirección local `/mcp` que aparece al arrancar. Usa `SWB_KEY_OWNER` del `.env` de la instancia como `Authorization: Bearer <key>`. Esta clave da acceso completo de propietario. Guárdala en la configuración local privada del cliente con el modo de archivo `0600`. Envíala solo al SwB local.

## Emparejar un cliente [#emparejar-un-cliente]

Deja que tu agente configure la app si puede editar sus ajustes en local. Guarda la clave en los ajustes privados de la app. Usa el modo de archivo `0600` para un archivo de ajustes.

### Dar nombre a la app [#dar-nombre-a-la-app]

Dile al agente un nombre corto para la app, por ejemplo `notes-bot`. SwB muestra este nombre en los mensajes de la app.
### Obtener el código de emparejamiento [#obtener-el-código-de-emparejamiento]

Pide a un agente propietario de SwB que llame a `connect_code` con el nombre de la app. También puedes ejecutar `uvx swb-one connect mcp-client --choose agent=notes-bot` desde la carpeta de la instancia de SwB. Cambia `notes-bot` por el nombre de tu app.
### Obtener la clave [#obtener-la-clave]

En el ordenador donde se ejecuta la app, abre una carpeta privada en tu terminal. Cambia `https://swb.example.com` por tu dirección de SwB. Mantén `/connect` al final. Para un SwB local, usa `http://localhost:8870/connect` con el puerto que aparece al arrancar. Escribe el código de emparejamiento cuando el comando lo pida, en un plazo de 10 minutos. Lo que escribes queda oculto.

```bash
bash -c '
  umask 077
  set -C
  read -rsp "Pairing code: " code || exit
  printf "\n"
  printf "{\"code\":\"%s\"}" "$code" |
    curl -fsS https://swb.example.com/connect -H "Content-Type: application/json" \
      --data-binary @- > .swb-pairing.json
'
```
### Mantener la clave en privado [#mantener-la-clave-en-privado]

Abre `.swb-pairing.json` en un editor privado. El comando guarda la respuesta con el modo de archivo `0600`. Usa su `key` en los ajustes de la app. SwB devuelve la clave una sola vez.
### Añadir SwB a la app [#añadir-swb-a-la-app]

Añade un servidor llamado `swb` en los ajustes MCP de la app. Selecciona Streamable HTTP. Usa el `mcp_url` del archivo para un servidor SwB público. Para un SwB local, usa la dirección MCP que aparece al arrancar.
### Añadir la clave [#añadir-la-clave]

Añade la cabecera `Authorization: Bearer <key>` en los ajustes privados de la app. Borra `.swb-pairing.json` después de guardar la clave.

## Lo que hace el agente [#lo-que-hace-el-agente]

El agente llama a `connect(what="mcp-client")` con el inicio de sesión del propietario. SwB devuelve la dirección MCP y pide el nombre de la app. El agente vuelve a llamar a `connect` con `choose={"agent":"notes-bot"}`.

SwB crea un código de emparejamiento para ese nombre y devuelve el paso `POST /connect`. Un código de emparejamiento es un código de un solo uso válido durante 10 minutos. Si la app ya tiene una clave, SwB lo indica y crea un código nuevo solo después de `connect_revoke`.

Desde la terminal del servidor SwB, `swb connect mcp-client --choose agent=notes-bot` da los mismos pasos.

## Credenciales [#credenciales]

| Elemento | Dónde aparece | Formato | Dónde va |
| - | - | - | - |
| Código de emparejamiento | El resultado de `connect` o `connect_code` | `XXXXX-XXXXX`, válido 10 minutos, un solo uso | La petición `POST /connect` |
| Clave de agente | La respuesta de `POST /connect`, una sola vez | Empieza por `swb_` | La cabecera `Authorization` en los ajustes privados de la app |

Guarda la clave en los ajustes privados de la app. SwB guarda un hash de la clave en lugar de la propia clave.

## Comprobar la conexión [#comprobar-la-conexión]

Pide al agente nuevo que llame a `inbox`. Pide al agente conectado como propietario que llame a `connect_list` y compruebe la hora de la última llamada de la clave nueva. Haz que el agente propietario envíe un mensaje de prueba a `agent:notes-bot`. Haz que el agente nuevo lo lea con `inbox` y responda con `reply`.

## Resolver problemas [#resolver-problemas]

Revisa la dirección de SwB si falla `POST /connect`. Pide al propietario un código nuevo si el código caducó o ya se usó. Si una clave se filtra, revócala con `connect_revoke`. A partir de ese momento, SwB rechaza las llamadas que usan esa clave.
