Skip to content

Studio: two ways past the stdio MCP UI-session gate - #8551

Merged
danielhanchen merged 3 commits into
mainfrom
studio-stdio-mcp-ui-session
Aug 12, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-stdio-mcp-ui-session

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

A stdio MCP server is a local command. The MCP client parses the stored address into argv and hands it to FastMCP's StdioTransport, which starts the process as the backend user, outside the tool sandbox. That is the intended feature for someone sitting at their own machine, and this PR keeps it that way: a loopback bind still defaults it on, so the MCP Servers dialog works with no env var, exactly as before.

What was not intended is the credential that could reach it.

The problem

Every route in routes/mcp_servers.py depends only on get_current_subject, which accepts an sk-unsloth API key as readily as the UI's session JWT. That key is the remote, long-lived, exportable one. We hand it out for programmatic access and for Cloudflare remote access, and the README says so plainly:

anyone with the link and API key can use it and run code: keep your API key private

So a leaked inference key could POST /api/mcp/servers/test with a command in the url field and have it spawned on the spot, with no storage round trip:

Authorization: Bearer sk-unsloth-...
{"url": "/bin/sh -c '<anything>'"}

Sandboxed tool execution and arbitrary local process execution are different grants, and only the first one was ever offered.

POST /api/data-recipe/mcp/tools was the same hole in a cleaner form: command, args and env come straight from the request body and are spawned immediately.

The fix

Gate the command paths on an interactive UI session, reusing require_ui_session from routes/provider_credentials.py rather than inventing a mechanism.

  • create, update, import, test and refresh now take authenticated_via_api_key and run a new _guard_stdio. The guard fires only when the address is a stdio command. An http(s) MCP server is data rather than code, so managing one stays open to API keys.
  • update keys off the resulting address or the stored row, not just the payload. Editing a stdio row is privileged even when no url is sent, since it can re-enable the row or rewrite the env the subprocess is handed.
  • import applies the guard per entry, inside the existing per-entry error handling, so an API key's http entries still import instead of the whole batch failing.
  • The guard runs after _validate_url, so a host with stdio disabled keeps answering 400 rather than 403. The response does not reveal whether the gate is open.
  • /api/data-recipe/mcp/tools gets the same rule, reported per provider so http providers in the same request still list.
  • delete and list are deliberately left open. One de-privileges, the other is read-only, and gating delete would strand rows for API-only clients.

Each registration or probe now writes one audit line carrying stdio_log_id (executable basename plus a short digest), never the raw address, the argv or the headers. Argv routinely carries credentials, and for a stdio row headers_json is the subprocess environment. _stdio_log_id loses its underscore so the routes can share that redaction.

What deliberately did not change

The loopback auto-default stays. Removing it would have meant every local Studio and Desktop user had to set an environment variable and restart before adding an MCP server, which fixes the hole by breaking the feature.

No frontend change. The MCP Servers dialog and the desktop app both authenticate with a session JWT, so a local user adding npx -y @modelcontextprotocol/server-filesystem /tmp sees no difference: same field, same Test connection button, no env var, no new prompt.

No other caller is affected. The CLI, the Tauri Rust side and the Colab notebook never call these routes, and the internal keys minted by Deep Research and data recipe jobs only ever reach /v1.

The chat path is untouched. An API key can still invoke stdio MCP tools that a UI session already registered, which is the documented "Studio as an API server with my tools" flow. --disable-tools and enabled_tools remain the lever there.

Nothing here is stateful: no new setting, no schema change, no migration, so re-running is a no-op.

Tests

291 pass across the MCP, tool policy and provider suites. New coverage:

  • 403 with no spawn for an API key on create, test, refresh, and a rename-only edit of a stdio row
  • a mixed import creating the http entry and reporting the stdio one, without failing the batch
  • the 400-not-403 ordering guard on a stdio-disabled host
  • delete of a stdio row still allowed, documenting the carve-out
  • test_http_management_still_open_to_api_keys: create, update, test and refresh on https://x/mcp all still succeed with an API key
  • a log hygiene test asserting a token in argv and the values in the env never reach the record

Route handlers are called directly in tests, so an omitted via_api_key receives the Depends sentinel, which is truthy and fails closed. Existing stdio call sites now pass via_api_key = False.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 12, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 12, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 4539470df0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Follow-up to #8550, which put local (stdio) MCP commands behind a UI
session on every route that defines one. Two paths still let an
sk-unsloth API key around it.

GET /api/mcp/servers/ returns stored rows verbatim. For a stdio row both
fields are secrets: url is the argv, which carries credentials often
enough that stdio_log_id exists so it is never logged raw, and headers is
the subprocess env. A key that may not define a command could still read
back one the owner had defined, tokens included. Listing now drops stdio
rows for API-key callers. Dropping the row rather than blanking its
fields also keeps a redacted url from round-tripping into update as a
bogus command. http(s) rows are untouched, headers included, and a UI
session still sees everything.

mcp_server.py calls the validate route function directly, so the
ViaApiKey dependency never runs and its `= False` default reads as a UI
session. The /mcp surface is a remote static bearer, so it could validate
a recipe carrying a stdio provider, which discovers that provider's tools
and starts the command. It now passes via_api_key = True explicitly.

Tests: the listing hides the stdio row from a key with the argv and env
secrets asserted absent from the serialized response, shows both to a
session, keeps http headers visible to a key, and pins the call site that
has to hand in the flag.
@danielhanchen
danielhanchen force-pushed the studio-stdio-mcp-ui-session branch from c415fda to 6172680 Compare August 12, 2026 13:11
@danielhanchen danielhanchen changed the title Studio: require a UI session to register or probe a stdio MCP server Studio: two ways past the stdio MCP UI-session gate Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 12, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 12, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 6172680969

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 8646a6573a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6d4a9ff into main Aug 12, 2026
47 of 53 checks passed
@danielhanchen
danielhanchen deleted the studio-stdio-mcp-ui-session branch August 12, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant