mirror of
https://github.com/coder/coder.git
synced 2026-11-25 14:17:56 +08:00
feea5ab959f59cd2135443ed2a0c19bf7643db8f
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
ba068876cd |
feat: register MCP OAuth2 clients when MCP server configs are updated (#31559)
Relates to CODAGT-779 We only ran Dynamic Client Registration when an MCP server config was created. If an admin later switched an existing server to OAuth2, or changed the URL of a server whose client Coder had registered, the server was left without a working client, and users couldn't connect to it. To fix, create and update now go through the same planner, which registers a client whenever a save needs one. That's when a config becomes `dynamic` (on create, when the auth type changes to OAuth2, or when `oauth2_client_registration` changes from `manual` to `dynamic`), or when the URL of a `dynamic` config changes. The update request gets an optional `oauth2_client_registration` field. When it's omitted, we use `dynamic` if the resulting config has no client ID, auth URL or token URL, and keep the stored value otherwise. Coder owns the client ID, secret, auth URL and token URL of a `dynamic` config, so we ignore them when a request leaves them empty or repeats them, and any other value fails with a 400 unless the request also sets `manual`. Updates that touch auth fields are also validated now, so a `manual` config without a client ID, auth URL or token URL is rejected. A new client replaces the client ID, so it deletes every user's stored token. If discovery or registration fails, nothing changes, and if the config changes whilst we're registering, the request fails with a `409 Conflict`. There are a few gaps I've left for now, and documented. A change to the deployment's access URL doesn't register a new client. Configs broken by the old behaviour stay `manual` until an admin switches them to `dynamic`. We also don't re-register when the provider rejects the client with `invalid_client`. |
||
|
|
fa7cd62c8b |
feat: send RFC 8707 resource parameter on MCP server OAuth2 flows (#30651)
Coder's MCP OAuth2 client never sent the RFC 8707 `resource` parameter, which the MCP authorization spec requires on authorization and token requests. Authorization servers that enforce it cannot be connected: with Dagster+, discovery and dynamic client registration succeed, then the authorize request fails with "MCP OAuth requests must include resource". MCP server configs get an `oauth2_resource` field (migration 000618, admin-only in API responses, audited). Coder sends it on the authorize redirect, the authorization-code exchange, and token refresh. Refresh now goes through `clientcredentials` with `grant_type=refresh_token`, because `oauth2.Config` cannot add parameters to refresh requests; it keeps the same auth-style detection and error types, and adds no dependency. Auto-discovery fills the field with the most specific Protected Resource Metadata `resource` that covers the server URL, stored as advertised. As in the MCP SDKs, a resource covers URLs on its origin at or under its path, so `/mcp` covers `/mcp/` but `/mcp/` does not cover `/mcp`. URLs compare in RFC 3986 normal form (default ports, percent-encodings, dot segments, and an empty path as `/`), ports ignore leading zeros, IPv6 literals compare by address, and `%2F` stays inside its segment. Unlike the SDKs, a resource with a query, even an empty one, only covers a server URL with the same query. When none covers it, Coder uses the server URL with the scheme and host lowercased and user information and fragment removed, or sends no resource when that is not a valid resource indicator. An explicit value in the create request wins. Manual configurations send no `resource` unless an admin sets one, because some providers reject it (Microsoft Entra ID v2 fails with AADSTS901002). Values must be absolute RFC 3986 URIs without a fragment, user information, or an IPv6 zone identifier. The resource binds grants like the other OAuth settings: changing it deletes stored user tokens, and a callback whose config resource changed after the user started connecting returns 409 before the code is redeemed. The connect step records the resource in a callback-scoped cookie for that check. Discovery only runs when a configuration is created, so existing auto-discovered configurations, including the Dagster+ one that prompted this, keep an empty resource until an admin sets the new "Resource URL" field or re-creates the server.  ## Testing - Go: discovery (most specific match, path and trailing-slash boundaries, query matching including empty queries, equivalent ports, IPv6 literals, escapes, and dot segments including escaped ones, the normalized fallback and an invalid one, explicit override), validation (URI characters, brackets, IP literals, user information, trimming), grant deletion on change, clearing on an auth type change, redaction for members, refresh with and without a resource, the 409 when the resource changes mid-flow (the authorization server never receives the code), and a connect and callback flow against an authorization server that rejects requests without the resource. Each new test was checked to fail when the behavior it covers is broken, except two malformed IP literal hosts that Go's URL parser already rejects. - Vitest: the form round-trips the stored value, and clearing the field sends `""` on update. - Remote UAT through Coder Agents ([chat](https://dogfood.cdr.dev/agents/58695d5b-2112-400b-88f7-ab64c678f756)) on a test deployment with loopback MCP and authorization server mocks: - Round 1 on |
||
|
|
50f36315fd |
fix(coderd): accept RFC 9728 resource metadata with array-valued resource (#28592)
GitLab's official MCP server returns `"resource"` as a JSON array in its `/.well-known/oauth-protected-resource` document, while RFC 9728 §2 defines it as a string. Coder's OAuth2 auto-discovery failed to decode the response (`json: cannot unmarshal array into Go struct field protectedResourceMetadata.resource of type string`), blocking zero-config `oauth2` DCR against GitLab. This makes `protectedResourceMetadata.Resource` tolerate both a single string and an array of strings. Part of [CODAGT-570](https://linear.app/codercom/issue/CODAGT-570/coder-agents-mcp-client-cannot-connect-to-gitlabs-official-mcp-server). The other blocker in that issue (GitLab replying 204 to `notifications/initialized`) was already fixed by the migration from `mark3labs/mcp-go` to `modelcontextprotocol/go-sdk`, whose streamable-HTTP client accepts 204. <details> <summary>Investigation notes</summary> CODAGT-570 reported two independent incompatibilities with GitLab CE's `/api/v4/mcp` server: 1. **204 vs 202 on `notifications/initialized` (blocked all auth modes):** the bundled `mark3labs/mcp-go` client only accepted 200/202 for notification POSTs. That library has since been removed entirely; `coderd/x/chatd/mcpclient` now uses `github.com/modelcontextprotocol/go-sdk v1.7.0`, which accepts both 204 and 202 (and only warns on other codes in non-strict mode, which is the only mode reachable from Coder). No further change needed. 2. **RFC 9728 `resource` array (blocked zero-config oauth2 DCR):** Coder's own parser in `coderd/mcp.go` declared `Resource string`. This lives outside the MCP library, so the SDK migration did not fix it. Fixed here with a custom `UnmarshalJSON` accepting string or array of strings. The field is only decoded, never consumed downstream, so behavior is otherwise unchanged. </details> --- Generated by Coder Agents on behalf of @f0ssel. |
||
|
|
d889ba1842 |
feat: add user_oidc auth type for MCP servers (#24793)
Adds a 5th MCP server authentication mode, `user_oidc` ("User OIDC
Identity"), that forwards the calling user's OIDC access token from
`user_links.oauth_access_token` to the upstream MCP server as
`Authorization: Bearer <token>`.
The token is read from `user_links` and refreshed transparently via
`oauth2.TokenSource` before each MCP request. No new per-MCP-server
secret storage and no per-user connect/disconnect step.
**Limitation**: only users who logged in via OIDC have a forwardable
token. Users authenticated via password or GitHub will see requests sent
without an `Authorization` header, and the upstream MCP server is
expected to respond with 401. A pluggable token source (e.g. CLI-minted
E2E tokens) is left as future work.
<details>
<summary>Implementation notes</summary>
- Schema: new
`coderd/database/migrations/000481_mcp_user_oidc_auth.{up,down}.sql`
relaxes the `mcp_server_configs.auth_type` CHECK constraint to include
`user_oidc`. Down migration deletes affected rows before restoring the
old constraint.
- SDK validation: `codersdk/mcp.go` extends `oneof` for
`CreateMCPServerConfigRequest` and `UpdateMCPServerConfigRequest`.
- Handler: `coderd/mcp.go` adds `case "user_oidc":` to the
field-clearing switch on update. The existing list and detail handlers
already report `auth_connected = true` for any non-`oauth2` auth type.
- Header construction: `coderd/x/chatd/mcpclient/mcpclient.go`
introduces a `UserOIDCTokenSource` interface and adds the `user_oidc`
case to `buildAuthHeaders`. `ConnectAll` / `connectOne` /
`buildAuthHeaders` gain `userID uuid.UUID, oidcSrc UserOIDCTokenSource`
parameters.
- Wiring: `coderd/x/chatd/chatd.go` adds `OIDCTokenSource` to `Config` /
`Server` and passes `chat.OwnerID` plus the source through `ConnectAll`.
`coderd/coderd.go` constructs the source next to the `chatd.New` call
when `options.OIDCConfig` is non-nil.
- Token source: `oidcMCPTokenSource` lives in `coderd/mcp.go`. It reads
the user's OIDC link, refreshes via `oauth2.TokenSource`, and writes the
refreshed token back to `user_links`. Logic is duplicated from
`provisionerdserver.ObtainOIDCAccessToken` to avoid an MCP ->
provisionerdserver dependency. The two copies must be kept in sync; a
comment on `oidcMCPTokenSource` records this.
- Frontend: `MCPServerAdminPanel.tsx` adds the new dropdown option, an
explanatory helper block (no admin-configurable fields), and a Storybook
story (`CreateServerUserOIDC`).
- Tests:
- `mcpclient_test.go`: `TestConnectAll_UserOIDCAuth`,
`TestConnectAll_UserOIDCAuth_NoLink`,
`TestConnectAll_UserOIDCAuth_NilSource`. All existing tests updated for
the new signature.
- `mcp_test.go`: extends `TestMCPServerConfigsAuthConnected` to assert
`auth_connected=true` for `user_oidc`; adds
`TestMCPServerConfigsUserOIDCClearsFields` and
`TestMCPServerConfigsUserOIDCDirect`.
- Docs: `docs/ai-coder/agents/platform-controls/mcp-servers.md`
describes the new mode and its OIDC-only limitation.
</details>
This PR was created by Coder Agents.
---------
Co-authored-by: Coder Agents <agents@coder.com>
|