mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add MCP server configuration backend for chats (#23227)
## 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/`
This commit is contained in:
@@ -73,6 +73,7 @@ const makeChat = (
|
||||
id,
|
||||
owner_id: "owner-1",
|
||||
last_model_config_id: "model-1",
|
||||
mcp_server_ids: [],
|
||||
title: `Chat ${id}`,
|
||||
status: "running",
|
||||
created_at: "2025-01-01T00:00:00.000Z",
|
||||
|
||||
Generated
+100
@@ -1070,6 +1070,7 @@ export interface Chat {
|
||||
readonly created_at: string;
|
||||
readonly updated_at: string;
|
||||
readonly archived: boolean;
|
||||
readonly mcp_server_ids: readonly string[];
|
||||
}
|
||||
|
||||
// From codersdk/deployment.go
|
||||
@@ -2076,6 +2077,7 @@ export interface ConvertLoginRequest {
|
||||
export interface CreateChatMessageRequest {
|
||||
readonly content: readonly ChatInputPart[];
|
||||
readonly model_config_id?: string;
|
||||
readonly mcp_server_ids?: string[];
|
||||
}
|
||||
|
||||
// From codersdk/chats.go
|
||||
@@ -2123,6 +2125,7 @@ export interface CreateChatRequest {
|
||||
readonly content: readonly ChatInputPart[];
|
||||
readonly workspace_id?: string;
|
||||
readonly model_config_id?: string;
|
||||
readonly mcp_server_ids?: readonly string[];
|
||||
}
|
||||
|
||||
// From codersdk/users.go
|
||||
@@ -2163,6 +2166,32 @@ export interface CreateGroupRequest {
|
||||
readonly quota_allowance: number;
|
||||
}
|
||||
|
||||
// From codersdk/mcp.go
|
||||
/**
|
||||
* CreateMCPServerConfigRequest is the request to create a new MCP server config.
|
||||
*/
|
||||
export interface CreateMCPServerConfigRequest {
|
||||
readonly display_name: string;
|
||||
readonly slug: string;
|
||||
readonly description: string;
|
||||
readonly icon_url: string;
|
||||
readonly transport: string;
|
||||
readonly url: string;
|
||||
readonly auth_type: string;
|
||||
readonly oauth2_client_id?: string;
|
||||
readonly oauth2_client_secret?: string;
|
||||
readonly oauth2_auth_url?: string;
|
||||
readonly oauth2_token_url?: string;
|
||||
readonly oauth2_scopes?: string;
|
||||
readonly api_key_header?: string;
|
||||
readonly api_key_value?: string;
|
||||
readonly custom_headers?: Record<string, string>;
|
||||
readonly tool_allow_list?: readonly string[];
|
||||
readonly tool_deny_list?: readonly string[];
|
||||
readonly availability: string;
|
||||
readonly enabled: boolean;
|
||||
}
|
||||
|
||||
// From codersdk/organizations.go
|
||||
export interface CreateOrganizationRequest {
|
||||
readonly name: string;
|
||||
@@ -3692,6 +3721,51 @@ export interface LoginWithPasswordResponse {
|
||||
readonly session_token: string;
|
||||
}
|
||||
|
||||
// From codersdk/mcp.go
|
||||
/**
|
||||
* MCPServerConfig represents an admin-configured MCP server.
|
||||
*/
|
||||
export interface MCPServerConfig {
|
||||
readonly id: string;
|
||||
readonly display_name: string;
|
||||
readonly slug: string;
|
||||
readonly description: string;
|
||||
readonly icon_url: string;
|
||||
readonly transport: string; // "streamable_http" or "sse"
|
||||
readonly url: string;
|
||||
readonly auth_type: string; // "none", "oauth2", "api_key", "custom_headers"
|
||||
/**
|
||||
* OAuth2 fields (only populated for admins).
|
||||
*/
|
||||
readonly oauth2_client_id?: string;
|
||||
readonly has_oauth2_secret: boolean;
|
||||
readonly oauth2_auth_url?: string;
|
||||
readonly oauth2_token_url?: string;
|
||||
readonly oauth2_scopes?: string;
|
||||
/**
|
||||
* API key fields (only populated for admins).
|
||||
*/
|
||||
readonly api_key_header?: string;
|
||||
readonly has_api_key: boolean;
|
||||
readonly has_custom_headers: boolean;
|
||||
/**
|
||||
* Tool governance.
|
||||
*/
|
||||
readonly tool_allow_list: readonly string[];
|
||||
readonly tool_deny_list: readonly string[];
|
||||
/**
|
||||
* Availability policy set by admin.
|
||||
*/
|
||||
readonly availability: string; // "force_on", "default_on", "default_off"
|
||||
readonly enabled: boolean;
|
||||
readonly created_at: string;
|
||||
readonly updated_at: string;
|
||||
/**
|
||||
* Per-user state (populated for non-admin requests).
|
||||
*/
|
||||
readonly auth_connected: boolean;
|
||||
}
|
||||
|
||||
// From codersdk/provisionerdaemons.go
|
||||
/**
|
||||
* MatchedProvisioners represents the number of provisioner daemons
|
||||
@@ -6844,6 +6918,32 @@ export interface UpdateInboxNotificationReadStatusResponse {
|
||||
readonly unread_count: number;
|
||||
}
|
||||
|
||||
// From codersdk/mcp.go
|
||||
/**
|
||||
* UpdateMCPServerConfigRequest is the request to update an MCP server config.
|
||||
*/
|
||||
export interface UpdateMCPServerConfigRequest {
|
||||
readonly display_name?: string;
|
||||
readonly slug?: string;
|
||||
readonly description?: string;
|
||||
readonly icon_url?: string;
|
||||
readonly transport?: string;
|
||||
readonly url?: string;
|
||||
readonly auth_type?: string;
|
||||
readonly oauth2_client_id?: string;
|
||||
readonly oauth2_client_secret?: string;
|
||||
readonly oauth2_auth_url?: string;
|
||||
readonly oauth2_token_url?: string;
|
||||
readonly oauth2_scopes?: string;
|
||||
readonly api_key_header?: string;
|
||||
readonly api_key_value?: string;
|
||||
readonly custom_headers?: Record<string, string>;
|
||||
readonly tool_allow_list?: string[];
|
||||
readonly tool_deny_list?: string[];
|
||||
readonly availability?: string;
|
||||
readonly enabled?: boolean;
|
||||
}
|
||||
|
||||
// From codersdk/notifications.go
|
||||
export interface UpdateNotificationTemplateMethod {
|
||||
readonly method?: string;
|
||||
|
||||
@@ -106,6 +106,7 @@ const baseChatFields = {
|
||||
owner_id: "owner-id",
|
||||
workspace_id: mockWorkspace.id,
|
||||
last_model_config_id: "model-config-1",
|
||||
mcp_server_ids: [],
|
||||
created_at: "2026-02-18T00:00:00.000Z",
|
||||
updated_at: "2026-02-18T00:00:00.000Z",
|
||||
archived: false,
|
||||
|
||||
@@ -183,6 +183,7 @@ const makeChat = (chatID: string): TypesGen.Chat => ({
|
||||
id: chatID,
|
||||
owner_id: "owner-1",
|
||||
last_model_config_id: "model-1",
|
||||
mcp_server_ids: [],
|
||||
title: "test",
|
||||
status: "running",
|
||||
created_at: "2025-01-01T00:00:00.000Z",
|
||||
|
||||
@@ -52,6 +52,7 @@ export const WithParentChat: Story = {
|
||||
id: "parent-chat-1",
|
||||
owner_id: "owner-id",
|
||||
last_model_config_id: "model-config-1",
|
||||
mcp_server_ids: [],
|
||||
title: "Set up CI/CD pipeline",
|
||||
status: "completed",
|
||||
last_error: null,
|
||||
|
||||
@@ -36,6 +36,7 @@ const buildChat = (overrides: Partial<TypesGen.Chat> = {}): TypesGen.Chat => ({
|
||||
title: "Help me refactor",
|
||||
status: "completed",
|
||||
last_model_config_id: "model-config-1",
|
||||
mcp_server_ids: [],
|
||||
created_at: oneWeekAgo,
|
||||
updated_at: oneWeekAgo,
|
||||
archived: false,
|
||||
|
||||
@@ -113,6 +113,7 @@ const buildChat = (overrides: Partial<Chat> = {}): Chat => ({
|
||||
title: "Agent",
|
||||
status: "completed",
|
||||
last_model_config_id: defaultModelConfigs[0].id,
|
||||
mcp_server_ids: [],
|
||||
created_at: oneWeekAgo,
|
||||
updated_at: oneWeekAgo,
|
||||
archived: false,
|
||||
|
||||
@@ -40,6 +40,7 @@ const buildChat = (overrides: Partial<Chat> = {}): Chat => ({
|
||||
title: "Agent",
|
||||
status: "completed",
|
||||
last_model_config_id: defaultModelConfigs[0].id,
|
||||
mcp_server_ids: [],
|
||||
created_at: oneWeekAgo,
|
||||
updated_at: oneWeekAgo,
|
||||
archived: false,
|
||||
|
||||
@@ -63,6 +63,7 @@ const buildChat = (overrides: Partial<Chat> = {}): Chat => ({
|
||||
updated_at: oneWeekAgo,
|
||||
archived: false,
|
||||
last_error: null,
|
||||
mcp_server_ids: [],
|
||||
...overrides,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user