Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ Reuse saved authorization while it remains valid. If a browser reports connectio

See the [self-hosted server instructions](docs/commands.md#self-hosted-server) for installation, stdio and HTTP setup, environment variables, and OAuth configuration.

HTTP mode listens on `127.0.0.1` only. To accept connections from other machines, pass `--host 0.0.0.0` or set `MCP_HOST=0.0.0.0`, and enable OAuth first if other people will connect.

> [!WARNING]
> **After 0.6.0, self-hosted HTTP clients must authenticate every MCP request.** With OAuth off, configure each client to send its own Socket API token as `Authorization: Bearer <token>`, starting with `initialize`. Requests without it return `401`, even when the server has `SOCKET_API_TOKEN` set. Local stdio setup is unchanged.

Expand Down
50 changes: 29 additions & 21 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Without OAuth, the server still asks for `SOCKET_API_TOKEN` at startup, but it n
"mcpServers": {
"socket-mcp": {
"type": "http",
"url": "http://localhost:3000",
"url": "http://127.0.0.1:3000",
"headers": {
"Authorization": "Bearer your-socket-api-token"
}
Expand All @@ -78,36 +78,44 @@ Without OAuth, the server still asks for `SOCKET_API_TOKEN` at startup, but it n
Claude Code:

```sh
claude mcp add --transport http socket-mcp http://localhost:3000 \
claude mcp add --transport http socket-mcp http://127.0.0.1:3000 \
--header "Authorization: Bearer your-socket-api-token"
```

To let people sign in instead of copying a token into their client, enable OAuth (below). A shared or hosted deployment should always run with OAuth.

The server listens on `http://localhost:3000/`. The MCP endpoint is `/` and the health endpoint is `/health`; every other path answers `404`.
The server listens on `http://127.0.0.1:3000/`. It binds the IPv4 loopback address only, so other machines cannot reach it. Use `127.0.0.1` rather than `localhost` in client URLs, since some clients resolve `localhost` to the IPv6 `::1` first. The MCP endpoint is `/` and the health endpoint is `/health`; every other path answers `404`.

To accept connections from other machines, for example inside a container or behind a reverse proxy on another host, pass `--host` or set `MCP_HOST`:

```sh
MCP_HTTP_MODE=true SOCKET_API_TOKEN=your-api-token socket-mcp --http --host 0.0.0.0
```

`--host` takes precedence over `MCP_HOST`. Use `0.0.0.0` for every IPv4 interface, `::` for every IPv4 and IPv6 interface, or one interface's address. The server logs a warning at startup when it binds a non-loopback address. Enable OAuth before you expose the server to other people.

The transport is stateless. Each `POST /` is self-contained, with no session to open, no `Mcp-Session-Id` header, and nothing to expire, so you can put several instances behind a load balancer without session affinity. `GET` and `DELETE` on the MCP endpoint answer `405`. Clients written against the 2025 protocol, which open with an `initialize` handshake, are still served.

Environment variables for HTTP mode:

| Variable | Required | Default | Description |
| ------------------------------------------ | ------------------------------------------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MCP_HTTP_MODE` | Yes, unless you pass `--http` | `false` | Set to `true` to serve HTTP instead of stdio. The `--http` CLI flag does the same thing. |
| `MCP_HOST` | No | All interfaces | Address to bind the HTTP server to, for example `127.0.0.1` to accept loopback connections only. |
| `MCP_PORT` | No | `3000` | Port to bind the HTTP server to. |
| `SOCKET_API_TOKEN` | To skip the startup prompt when HTTP OAuth is off | None | Each HTTP caller sends its own token. Local stdio can use this variable as a fallback to `socket-mcp login`. See the aliases below. |
| `SOCKET_OAUTH_ISSUER` | With OAuth | None | OAuth issuer URL. Must be `https` on a public host. See "Enabling OAuth" below. |
| `SOCKET_OAUTH_INTROSPECTION_CLIENT_ID` | With OAuth | None | Client ID used for RFC 7662 token introspection. |
| `SOCKET_OAUTH_INTROSPECTION_CLIENT_SECRET` | With OAuth | None | Client secret used for RFC 7662 token introspection. |
| `SOCKET_OAUTH_REQUIRED_SCOPES` | No | None | Scopes this resource advertises and enforces, separated by spaces or commas. Each tool call is checked only for the listed scopes that tool needs; any active token can run `initialize` and `tools/list`. When unset, any active token passes. |
| `SOCKET_API_BASE_URL` | No | None | Override the upstream Socket API endpoint. Falls back to `https://api.socket.dev` when unset. |
| `SOCKET_MCP_OAUTH_BASE_URL` | No | API URL | Override the issuer used by local stdio OAuth. By default, OAuth follows `SOCKET_API_BASE_URL`; an `api.` hostname maps to its matching `mcp.` resource host. Non-loopback URLs must use HTTPS. |
| `SOCKET_DEBUG` | No | `false` | Turns on verbose request and cache tracing on stderr, points `depscore` at `http://localhost:8866`, and permits a local OAuth issuer. |
| `SOCKET_MCP_APPS_CHALLENGE_TOKEN` | For domain verification | None | Token the OpenAI plugin portal issues for domain verification. When set, `GET /.well-known/openai-apps-challenge` returns it as plain text; when unset, that path returns `404`. |
| `SOCKET_MCP_SENTRY_DSN` | No | None | Sentry DSN for error reporting; it must use HTTPS. When unset, reporting is off: the Sentry SDK is never loaded and nothing is sent. When set, HTTP mode sends scrubbed events for server-side failures. See [Error reporting](service.md#error-reporting). |
| `SOCKET_MCP_SENTRY_ENVIRONMENT` | No | `production` | Sentry `environment` reported events carry, such as `staging`. Read only when `SOCKET_MCP_SENTRY_DSN` is set. |
| `SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE` | No | `0` | Share of `tools/call` requests sent to Sentry as transactions, from 0 to 1. `0`, unset or out of range sends none. Read only when `SOCKET_MCP_SENTRY_DSN` is set. See [Tracing](service.md#tracing). |
| `TRUST_PROXY` | No | `false` | Trust `X-Forwarded-Host` and `X-Forwarded-Proto` when building OAuth metadata URLs. Enable only behind a reverse proxy that rewrites them. |
| Variable | Required | Default | Description |
| ------------------------------------------ | ------------------------------------------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MCP_HTTP_MODE` | Yes, unless you pass `--http` | `false` | Set to `true` to serve HTTP instead of stdio. The `--http` CLI flag does the same thing. |
| `MCP_HOST` | No | `127.0.0.1` | Address to bind the HTTP server to. Set `0.0.0.0` or `::` to accept connections from other machines. The `--host` CLI flag overrides it. |
| `MCP_PORT` | No | `3000` | Port to bind the HTTP server to. |
| `SOCKET_API_TOKEN` | To skip the startup prompt when HTTP OAuth is off | None | Each HTTP caller sends its own token. Local stdio can use this variable as a fallback to `socket-mcp login`. See the aliases below. |
| `SOCKET_OAUTH_ISSUER` | With OAuth | None | OAuth issuer URL. Must be `https` on a public host. See "Enabling OAuth" below. |
| `SOCKET_OAUTH_INTROSPECTION_CLIENT_ID` | With OAuth | None | Client ID used for RFC 7662 token introspection. |
| `SOCKET_OAUTH_INTROSPECTION_CLIENT_SECRET` | With OAuth | None | Client secret used for RFC 7662 token introspection. |
| `SOCKET_OAUTH_REQUIRED_SCOPES` | No | None | Scopes this resource advertises and enforces, separated by spaces or commas. Each tool call is checked only for the listed scopes that tool needs; any active token can run `initialize` and `tools/list`. When unset, any active token passes. |
| `SOCKET_API_BASE_URL` | No | None | Override the upstream Socket API endpoint. Falls back to `https://api.socket.dev` when unset. |
| `SOCKET_MCP_OAUTH_BASE_URL` | No | API URL | Override the issuer used by local stdio OAuth. By default, OAuth follows `SOCKET_API_BASE_URL`; an `api.` hostname maps to its matching `mcp.` resource host. Non-loopback URLs must use HTTPS. |
| `SOCKET_DEBUG` | No | `false` | Turns on verbose request and cache tracing on stderr, points `depscore` at `http://localhost:8866`, and permits a local OAuth issuer. |
| `SOCKET_MCP_APPS_CHALLENGE_TOKEN` | For domain verification | None | Token the OpenAI plugin portal issues for domain verification. When set, `GET /.well-known/openai-apps-challenge` returns it as plain text; when unset, that path returns `404`. |
| `SOCKET_MCP_SENTRY_DSN` | No | None | Sentry DSN for error reporting; it must use HTTPS. When unset, reporting is off: the Sentry SDK is never loaded and nothing is sent. When set, HTTP mode sends scrubbed events for server-side failures. See [Error reporting](service.md#error-reporting). |
| `SOCKET_MCP_SENTRY_ENVIRONMENT` | No | `production` | Sentry `environment` reported events carry, such as `staging`. Read only when `SOCKET_MCP_SENTRY_DSN` is set. |
| `SOCKET_MCP_SENTRY_TRACES_SAMPLE_RATE` | No | `0` | Share of `tools/call` requests sent to Sentry as transactions, from 0 to 1. `0`, unset or out of range sends none. Read only when `SOCKET_MCP_SENTRY_DSN` is set. See [Tracing](service.md#tracing). |
| `TRUST_PROXY` | No | `false` | Trust `X-Forwarded-Host` and `X-Forwarded-Proto` when building OAuth metadata URLs. Enable only behind a reverse proxy that rewrites them. |

The three OAuth variables are a set: configure all three or none. Setting only some of them makes the server print `Incomplete OAuth configuration for HTTP mode` and exit `1`, rather than quietly start unauthenticated.

Expand Down
39 changes: 38 additions & 1 deletion lib/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { serveStdio } from '@modelcontextprotocol/server/stdio'
import { errorMessage } from '@socketsecurity/lib/errors/message'

import {
DEFAULT_MCP_HOST,
getMcpHttpMode,
getMcpHost,
getMcpPort,
Expand Down Expand Up @@ -47,6 +48,8 @@ export interface SocketMcpCliDeps {
readonly apiToken: string | undefined
readonly argv: readonly string[]
readonly envHttpMode: boolean
// MCP_HOST, undefined when unset. `--host` overrides it; with neither, HTTP
// mode binds `DEFAULT_MCP_HOST` (loopback).
readonly host: string | undefined
readonly loadOAuthMetadata: () => Promise<unknown>
readonly port: number
Expand Down Expand Up @@ -103,6 +106,32 @@ export function createSocketMcpCliDeps(): SocketMcpCliDeps {
}
}

export const MISSING_HOST_VALUE_MSG =
'--host needs an address, for example `--host 0.0.0.0` to listen on every interface.'

/**
* Read `--host <address>` or `--host=<address>` from argv. Answers undefined
* when the flag is absent and null when it is present without a value.
*/
export function parseHostFlag(
argv: readonly string[],
): string | null | undefined {
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i]!
if (arg === '--host') {
const value = argv[i + 1]
return value === undefined || value === '' || value.startsWith('--')
? null
: value
}
if (arg.startsWith('--host=')) {
const value = arg.slice('--host='.length)
return value === '' ? null : value
}
}
return undefined
}

// The stdio transport's `onerror` sink.
export function logStdioTransportError(error: unknown): void {
logger.error(`Socket MCP stdio error: ${errorMessage(error)}`)
Expand Down Expand Up @@ -154,6 +183,14 @@ export async function runSocketMcpCli(deps: SocketMcpCliDeps): Promise<number> {
// it.
const useHttp = deps.envHttpMode || deps.argv.includes('--http')

// `--host` beats MCP_HOST; with neither, HTTP mode binds loopback only.
const hostFlag = parseHostFlag(deps.argv)
if (useHttp && hostFlag === null) {
logger.error(MISSING_HOST_VALUE_MSG)
return 1
}
const httpHost = hostFlag ?? deps.host ?? DEFAULT_MCP_HOST

// OAuth is an HTTP-mode-only posture, so a truthy result also means
// `useHttp`.
const oauthResult = useHttp ? deps.setOauthEnabled() : undefined
Expand Down Expand Up @@ -227,7 +264,7 @@ export async function runSocketMcpCli(deps: SocketMcpCliDeps): Promise<number> {
if (!oauthResult) {
logger.info(HTTP_API_TOKEN_AUTH_MSG)
}
deps.startHttpServer(deps.port, deps.host)
deps.startHttpServer(deps.port, httpHost)
return 0
}

Expand Down
9 changes: 7 additions & 2 deletions lib/env.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ export function getMcpPort(): number {
return envInt('MCP_PORT') ?? 3000
}

// Unset keeps Node's default of listening on every interface, which deployed
// servers rely on. Set it (for example to 127.0.0.1) to bind one address.
// HTTP mode binds loopback unless the operator opts in to another address,
// so a local `--http` server is not reachable from the network by default.
export const DEFAULT_MCP_HOST = '127.0.0.1'

// The raw MCP_HOST value, undefined when unset. The CLI resolves the bind
// address from `--host`, then this, then `DEFAULT_MCP_HOST`. Set it to
// 0.0.0.0 (or ::) to listen on every interface.
export function getMcpHost(): string | undefined {
return envString('MCP_HOST')
}
Expand Down
Loading
Loading