# MCP (https://docs.ctxpipe.ai/docs/self-hosting/mcp)

Self-hosted ctxpipe exposes the same organization-scoped Streamable HTTP endpoint as
hosted ctxpipe:

```text
{AUTH_BASE_URL}/mcp
```

Every client uses this one URL. During authorization, the user chooses the
organizations that the client can read. Each tool call names one organization in
the `org` argument. The server accepts the old `?orgSlug=` parameter and ignores
it.

`AUTH_BASE_URL` must be the public origin clients use, including TLS. Client
JSON shapes and OAuth install paths are the same as on hosted ctxpipe. See
[Connect your agent](/docs/mcp/mcp-docs) and
[Authentication](/docs/self-hosting/authentication).

This page is for operators. Confirm that the endpoint is reachable. Then debug
OAuth and tools on a **test organization**.

<Callout type="warn" title="Ready for OAuth is not authenticated proof">
  `npx ctxpipe doctor mcp` checks HTTP routing, TLS or reachability, the
  unauthenticated Bearer challenge, and OAuth discovery. A `ready-for-oauth`
  result does **not** prove that browser OAuth, authenticated `tools/list`, or
  an MCP tool call work. The command does not accept OAuth bearer tokens and
  does not test STDIO servers. When `CTXPIPE_API_KEY` is set, it sends the key
  as `x-api-key` and checks an authenticated initialize.
</Callout>

## Connect a client

Use the same config as [Quickstart](/docs/self-hosting/quickstart), with your
deployment origin:

```json
{
  "mcpServers": {
    "ctxpipe": {
      "type": "http",
      "url": "https://ctx.example.com/mcp"
    }
  }
}
```

The MCP client must complete OAuth against this deployment. CLI setup sign-in
and MCP client OAuth are separate flows.

## Diagnose discovery

Run the lightweight doctor against the public MCP URL:

```bash
npx ctxpipe doctor mcp \
  --url "$AUTH_BASE_URL/mcp"

npx ctxpipe doctor mcp \
  --url "$AUTH_BASE_URL/mcp" \
  --json
```

`--json` is machine-readable. Failures set a non-zero exit code. Loopback HTTP
works with a warning. Remote endpoints must use HTTPS.

| Check                | Passes when                                                                                                                             |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| Target               | Path is `/mcp`; remote origins use HTTPS                                                                                                |
| Backend status       | `GET /.status` returns JSON with `status: "ok"`                                                                                         |
| OAuth challenge      | Unauthenticated Streamable HTTP `initialize` returns `401` with a Bearer `WWW-Authenticate` challenge that includes `resource_metadata` |
| Protected resource   | RFC 9728 metadata matches the `/mcp` audience and names an authorization server                                                         |
| Authorization server | RFC 8414 metadata exposes authorization, token, and registration endpoints                                                              |

Overall status is `ready-for-oauth`, `ready-for-api-key` (with
`CTXPIPE_API_KEY` set), `warning`, or `failed`.

If TLS fails from a Node CLI against a local CA (Portless or a private CA), set
`NODE_EXTRA_CA_CERTS` to the CA file. Do not disable TLS verification.

## Authenticated checks with MCPJam

MCPJam is an external diagnostic client. It is not bundled into `ctxpipe` or the
self-hosted backend. Pin the versions below. Do not use `@latest`. Use a test
organization. Do not use a customer organization.

```bash
npx @mcpjam/cli@3.24.0 oauth login \
  --url "$AUTH_BASE_URL/mcp" \
  --protocol-version 2025-11-25 \
  --registration dcr \
  --credentials-out .mcpjam/credentials.json \
  --debug-out .mcpjam/oauth-debug.json

npx @mcpjam/cli@3.24.0 tools list \
  --url "$AUTH_BASE_URL/mcp" \
  --credentials-file .mcpjam/credentials.json

npx @mcpjam/cli@3.24.0 tools call \
  --url "$AUTH_BASE_URL/mcp" \
  --credentials-file .mcpjam/credentials.json \
  --tool-name list_workspaces \
  --tool-args '{}'
```

For a visual OAuth and JSON-RPC timeline on a local origin:

```bash
npx @mcpjam/inspector@2.40.0 \
  --url "$AUTH_BASE_URL/mcp" \
  --name "ctxpipe self-host" \
  --oauth \
  --tab servers
```

The hosted MCPJam application only reaches public HTTPS. It cannot use
`localhost` or refresh against a local authorization server.

## Artifacts and credentials

Keep OAuth credentials and debug traces out of Git. Write them under `.mcpjam/`
(gitignored in the ctxpipe repository). Review files before sharing. The
credentials file can include an access token, refresh token, client id, and
client secret even when stdout is redacted.

The ctxpipe doctor never accepts or prints OAuth bearer tokens, and never
prints an API key. Do not put `access_token`
on the MCP URL.

MCPJam does not prove token refresh races, dropped-session recovery, or
concurrent clients. Do not gate CI on interactive OAuth or LLM evals.

## Common failures

* **`org_not_found` or `org_required`** — the tool call named an organization
  that is not in the connection, an organization that the user is not a member
  of, or no organization. Pass the right slug in `org`. For an OAuth connection,
  add the organization on **Connected apps**, or reconnect the client and select
  the organization during authorization.
* **`AUTH_BASE_URL` mismatch** — issuer, callbacks, and MCP discovery must use
  the same public origin clients see.
* **Remote HTTP** — production MCP must be HTTPS.
* **Status unreachable or timed out** — backend, DNS, TLS, or an untrusted CA.
* **HTTP 401 without `resource_metadata`** — clients cannot start OAuth discovery.
* **Protected-resource audience mismatch** — RFC 9728 `resource` canonicalizes to
  `/mcp` without the query string.
* **Authorization-server metadata missing registration** — dynamic client
  registration cannot proceed.
* **CLI setup sign-in worked, but the MCP client still asks for authorization** —
  expected. Setup auth and MCP OAuth are separate.

## Related pages

<Cards>
  <Card title="Quickstart" href="/docs/self-hosting/quickstart" description="First client config against your origin." />

  <Card title="Authentication" href="/docs/self-hosting/authentication" description="OAuth provider, audiences, and trusted origins." />

  <Card title="Operations" href="/docs/self-hosting/operations" description="Health checks and recovery." />

  <Card title="Connect your agent" href="/docs/mcp/mcp-docs" description="Client JSON shapes and hosted install paths." />
</Cards>
