mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
d8ff67fb68
## Summary
Adds the database schema, API endpoints, SDK types, and encryption
wrappers for admin-managed MCP (Model Context Protocol) server
configurations that chatd can consume. This is the backend foundation
for allowing external MCP tools (Sentry, Linear, GitHub, etc.) to be
used during AI chat sessions.
## Database
Two new tables:
- **`mcp_server_configs`**: Admin-managed server definitions with URL,
transport (Streamable HTTP / SSE), auth config (none / OAuth2 / API key
/ custom headers), tool allow/deny lists, and an availability policy
(`force_on` / `default_on` / `default_off`). Includes CHECK constraints
on transport, auth_type, and availability values.
- **`mcp_server_user_tokens`**: Per-user OAuth2 tokens for servers
requiring individual authentication. Cascades on user/config deletion.
New column on `chats` table:
- **`mcp_server_ids UUID[]`**: Per-chat MCP server selection, following
the same pattern as `model_config_id` — passed at chat creation,
changeable per-message with nil-means-no-change semantics.
## API Endpoints
All routes are under `/api/experimental/mcp/servers/` and gated behind
the `agents` experiment.
**Admin endpoints** (`ResourceDeploymentConfig` auth):
- `POST /` — Create MCP server config
- `PATCH /{id}` — Update MCP server config (full-replace)
- `DELETE /{id}` — Delete MCP server config
**Authenticated endpoints** (all users, enabled servers only for
non-admins):
- `GET /` — List configs (admins see all, members see enabled-only with
admin fields redacted)
- `GET /{id}` — Get config by ID (with `auth_connected` populated
per-user)
**OAuth2 per-user auth flow:**
- `GET /{id}/oauth2/connect` — Initiate OAuth2 flow (state cookie CSRF
protection)
- `GET /{id}/oauth2/callback` — Handle OAuth2 callback, store tokens
- `DELETE /{id}/oauth2/disconnect` — Remove stored OAuth2 tokens
## Security
- **Secrets never returned**: `OAuth2ClientSecret`, `APIKeyValue`, and
`CustomHeaders` are never in API responses — only boolean indicators
(`has_oauth2_secret`, `has_api_key`, `has_custom_headers`).
- **Field redaction for non-admins**: `convertMCPServerConfigRedacted`
strips `OAuth2ClientID`, auth URLs, scopes, and `APIKeyHeader` from
non-admin responses.
- **dbcrypt encryption at rest**: All 5 secret fields use `dbcrypt_keys`
encryption with full encrypt-on-write / decrypt-on-read wrappers (11
dbcrypt method overrides + 2 helpers), following the same pattern as
`chat_providers.api_key`.
- **OAuth2 CSRF protection**: State parameter stored in `HttpOnly`
cookie with `HTTPCookies.Apply()` for correct `Secure`/`SameSite` behind
TLS-terminating proxies.
- **dbauthz authorization**: All 18 querier methods have authorization
wrappers. Read operations use `ActionRead`, write operations use
`ActionUpdate` on `ResourceDeploymentConfig`.
## Governance Model
| Control | Implementation |
|---------|---------------|
| **Global kill switch** | `enabled` defaults to `false` |
| **Availability policy** | `force_on` (always injected), `default_on`
(pre-selected), `default_off` (opt-in) |
| **Per-chat selection** | `mcp_server_ids` on `CreateChatRequest` /
`CreateChatMessageRequest` |
| **Auth gate** | OAuth2 servers require per-user auth before tools are
injected |
| **Tool-level allow/deny** | Arrays on `mcp_server_configs` for
granular tool filtering |
| **Secrets encrypted at rest** | Uses `dbcrypt_keys` (same pattern as
`chat_providers.api_key`) |
## Tests
8 test functions covering:
- Full CRUD lifecycle (create, list, update, delete)
- Non-admin visibility filtering (enabled-only, field redaction)
- `auth_connected` population for OAuth2 vs non-OAuth2 servers
- Availability policy validation (valid values + invalid rejection)
- Unique slug enforcement (409 Conflict)
- OAuth2 disconnect idempotency
- Chat creation with `mcp_server_ids` persistence
## Known Limitations (Deferred)
These are documented and intentional for an experimental feature:
- **Audit logging** not yet wired — will add when feature stabilizes
- **Cross-field validation** (e.g., OAuth2 fields required when
`auth_type=oauth2`) — admin-only endpoint, will add when stabilizing
- **`force_on` auto-injection** — query exists but not yet wired into
chatd tool injection (follow-up)
- **Additional test coverage** — 403 auth tests, GET-by-ID tests,
callback CSRF tests planned for follow-up
## What's NOT in this PR
- Frontend UI (admin panel + chat picker)
- Actual MCP client connections (`chatd/chatmcp/` manager)
- Tool injection into `chatloop/`
192 lines
7.4 KiB
Go
192 lines
7.4 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// MCPServerOAuth2ConnectURL returns the URL the user should visit to
|
|
// start the OAuth2 flow for an MCP server. The frontend opens this
|
|
// in a new window/popup.
|
|
func (c *Client) MCPServerOAuth2ConnectURL(id uuid.UUID) string {
|
|
return fmt.Sprintf("%s/api/experimental/mcp/servers/%s/oauth2/connect", c.URL.String(), id)
|
|
}
|
|
|
|
// MCPServerOAuth2Disconnect removes the user's OAuth2 token for an
|
|
// MCP server.
|
|
func (c *Client) MCPServerOAuth2Disconnect(ctx context.Context, id uuid.UUID) error {
|
|
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/experimental/mcp/servers/%s/oauth2/disconnect", id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return ReadBodyAsError(res)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MCPServerConfig represents an admin-configured MCP server.
|
|
type MCPServerConfig struct {
|
|
ID uuid.UUID `json:"id" format:"uuid"`
|
|
DisplayName string `json:"display_name"`
|
|
Slug string `json:"slug"`
|
|
Description string `json:"description"`
|
|
IconURL string `json:"icon_url"`
|
|
|
|
Transport string `json:"transport"` // "streamable_http" or "sse"
|
|
URL string `json:"url"`
|
|
|
|
AuthType string `json:"auth_type"` // "none", "oauth2", "api_key", "custom_headers"
|
|
|
|
// OAuth2 fields (only populated for admins).
|
|
OAuth2ClientID string `json:"oauth2_client_id,omitempty"`
|
|
HasOAuth2Secret bool `json:"has_oauth2_secret"`
|
|
OAuth2AuthURL string `json:"oauth2_auth_url,omitempty"`
|
|
OAuth2TokenURL string `json:"oauth2_token_url,omitempty"`
|
|
OAuth2Scopes string `json:"oauth2_scopes,omitempty"`
|
|
|
|
// API key fields (only populated for admins).
|
|
APIKeyHeader string `json:"api_key_header,omitempty"`
|
|
HasAPIKey bool `json:"has_api_key"`
|
|
|
|
HasCustomHeaders bool `json:"has_custom_headers"`
|
|
|
|
// Tool governance.
|
|
ToolAllowList []string `json:"tool_allow_list"`
|
|
ToolDenyList []string `json:"tool_deny_list"`
|
|
|
|
// Availability policy set by admin.
|
|
Availability string `json:"availability"` // "force_on", "default_on", "default_off"
|
|
|
|
Enabled bool `json:"enabled"`
|
|
CreatedAt time.Time `json:"created_at" format:"date-time"`
|
|
UpdatedAt time.Time `json:"updated_at" format:"date-time"`
|
|
|
|
// Per-user state (populated for non-admin requests).
|
|
AuthConnected bool `json:"auth_connected"`
|
|
}
|
|
|
|
// CreateMCPServerConfigRequest is the request to create a new MCP server config.
|
|
type CreateMCPServerConfigRequest struct {
|
|
DisplayName string `json:"display_name" validate:"required"`
|
|
Slug string `json:"slug" validate:"required"`
|
|
Description string `json:"description"`
|
|
IconURL string `json:"icon_url"`
|
|
|
|
Transport string `json:"transport" validate:"required,oneof=streamable_http sse"`
|
|
URL string `json:"url" validate:"required,url"`
|
|
|
|
AuthType string `json:"auth_type" validate:"required,oneof=none oauth2 api_key custom_headers"`
|
|
OAuth2ClientID string `json:"oauth2_client_id,omitempty"`
|
|
OAuth2ClientSecret string `json:"oauth2_client_secret,omitempty"`
|
|
OAuth2AuthURL string `json:"oauth2_auth_url,omitempty" validate:"omitempty,url"`
|
|
OAuth2TokenURL string `json:"oauth2_token_url,omitempty" validate:"omitempty,url"`
|
|
OAuth2Scopes string `json:"oauth2_scopes,omitempty"`
|
|
APIKeyHeader string `json:"api_key_header,omitempty"`
|
|
APIKeyValue string `json:"api_key_value,omitempty"`
|
|
CustomHeaders map[string]string `json:"custom_headers,omitempty"`
|
|
|
|
ToolAllowList []string `json:"tool_allow_list,omitempty"`
|
|
ToolDenyList []string `json:"tool_deny_list,omitempty"`
|
|
|
|
Availability string `json:"availability" validate:"required,oneof=force_on default_on default_off"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
// UpdateMCPServerConfigRequest is the request to update an MCP server config.
|
|
type UpdateMCPServerConfigRequest struct {
|
|
DisplayName *string `json:"display_name,omitempty"`
|
|
Slug *string `json:"slug,omitempty"`
|
|
Description *string `json:"description,omitempty"`
|
|
IconURL *string `json:"icon_url,omitempty"`
|
|
|
|
Transport *string `json:"transport,omitempty" validate:"omitempty,oneof=streamable_http sse"`
|
|
URL *string `json:"url,omitempty" validate:"omitempty,url"`
|
|
|
|
AuthType *string `json:"auth_type,omitempty" validate:"omitempty,oneof=none oauth2 api_key custom_headers"`
|
|
OAuth2ClientID *string `json:"oauth2_client_id,omitempty"`
|
|
OAuth2ClientSecret *string `json:"oauth2_client_secret,omitempty"`
|
|
OAuth2AuthURL *string `json:"oauth2_auth_url,omitempty" validate:"omitempty,url"`
|
|
OAuth2TokenURL *string `json:"oauth2_token_url,omitempty" validate:"omitempty,url"`
|
|
OAuth2Scopes *string `json:"oauth2_scopes,omitempty"`
|
|
APIKeyHeader *string `json:"api_key_header,omitempty"`
|
|
APIKeyValue *string `json:"api_key_value,omitempty"`
|
|
CustomHeaders *map[string]string `json:"custom_headers,omitempty"`
|
|
|
|
ToolAllowList *[]string `json:"tool_allow_list,omitempty"`
|
|
ToolDenyList *[]string `json:"tool_deny_list,omitempty"`
|
|
|
|
Availability *string `json:"availability,omitempty" validate:"omitempty,oneof=force_on default_on default_off"`
|
|
Enabled *bool `json:"enabled,omitempty"`
|
|
}
|
|
|
|
func (c *Client) MCPServerConfigs(ctx context.Context) ([]MCPServerConfig, error) {
|
|
res, err := c.Request(ctx, http.MethodGet, "/api/experimental/mcp/servers", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, ReadBodyAsError(res)
|
|
}
|
|
var configs []MCPServerConfig
|
|
return configs, json.NewDecoder(res.Body).Decode(&configs)
|
|
}
|
|
|
|
func (c *Client) MCPServerConfigByID(ctx context.Context, id uuid.UUID) (MCPServerConfig, error) {
|
|
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/experimental/mcp/servers/%s", id), nil)
|
|
if err != nil {
|
|
return MCPServerConfig{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return MCPServerConfig{}, ReadBodyAsError(res)
|
|
}
|
|
var config MCPServerConfig
|
|
return config, json.NewDecoder(res.Body).Decode(&config)
|
|
}
|
|
|
|
func (c *Client) CreateMCPServerConfig(ctx context.Context, req CreateMCPServerConfigRequest) (MCPServerConfig, error) {
|
|
res, err := c.Request(ctx, http.MethodPost, "/api/experimental/mcp/servers", req)
|
|
if err != nil {
|
|
return MCPServerConfig{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusCreated {
|
|
return MCPServerConfig{}, ReadBodyAsError(res)
|
|
}
|
|
var config MCPServerConfig
|
|
return config, json.NewDecoder(res.Body).Decode(&config)
|
|
}
|
|
|
|
func (c *Client) UpdateMCPServerConfig(ctx context.Context, id uuid.UUID, req UpdateMCPServerConfigRequest) (MCPServerConfig, error) {
|
|
res, err := c.Request(ctx, http.MethodPatch, fmt.Sprintf("/api/experimental/mcp/servers/%s", id), req)
|
|
if err != nil {
|
|
return MCPServerConfig{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return MCPServerConfig{}, ReadBodyAsError(res)
|
|
}
|
|
var config MCPServerConfig
|
|
return config, json.NewDecoder(res.Body).Decode(&config)
|
|
}
|
|
|
|
func (c *Client) DeleteMCPServerConfig(ctx context.Context, id uuid.UUID) error {
|
|
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/experimental/mcp/servers/%s", id), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return ReadBodyAsError(res)
|
|
}
|
|
return nil
|
|
}
|