|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { removeTrailingSlashes } from "../common/url"; |
| 4 | + |
| 5 | +import type { CoderApi } from "../api/coderApi"; |
| 6 | +import type { SessionData, SessionState } from "../deployment/sessionStore"; |
| 7 | +import type { Logger } from "../logging/logger"; |
| 8 | + |
| 9 | +const MCP_SERVER_PROVIDER_ID = "coder"; |
| 10 | +const MCP_ENDPOINT = "/api/experimental/mcp/http"; |
| 11 | +const ENABLED_SETTING = "coder.mcpServer.enabled"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Registers the Coder MCP server provider. Returns undefined when the editor |
| 15 | + * lacks the MCP API, as some VS Code forks do. |
| 16 | + */ |
| 17 | +export function registerMcpServerProvider( |
| 18 | + client: Pick<CoderApi, "getExperiments" | "getAxiosInstance">, |
| 19 | + sessionState: SessionState, |
| 20 | + logger: Logger, |
| 21 | +): vscode.Disposable | undefined { |
| 22 | + const provider = new McpServerProvider(client, sessionState, logger); |
| 23 | + try { |
| 24 | + const registration = vscode.lm.registerMcpServerDefinitionProvider( |
| 25 | + MCP_SERVER_PROVIDER_ID, |
| 26 | + provider, |
| 27 | + ); |
| 28 | + return { |
| 29 | + dispose: () => { |
| 30 | + registration.dispose(); |
| 31 | + provider.dispose(); |
| 32 | + }, |
| 33 | + }; |
| 34 | + } catch (error) { |
| 35 | + provider.dispose(); |
| 36 | + logger.info("This editor does not support MCP server definitions", error); |
| 37 | + return undefined; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Offers the deployment's hosted MCP server, when it serves one, to VS Code |
| 43 | + * chat and the Agents window. VS Code signs in to it with its own OAuth flow, |
| 44 | + * so the definition carries no credentials. |
| 45 | + */ |
| 46 | +export class McpServerProvider |
| 47 | + implements vscode.McpServerDefinitionProvider, vscode.Disposable |
| 48 | +{ |
| 49 | + readonly #onDidChange = new vscode.EventEmitter<void>(); |
| 50 | + readonly #disposables: vscode.Disposable[] = []; |
| 51 | + #serverUrl: string | undefined; |
| 52 | + |
| 53 | + public readonly onDidChangeMcpServerDefinitions = this.#onDidChange.event; |
| 54 | + |
| 55 | + public constructor( |
| 56 | + private readonly client: Pick< |
| 57 | + CoderApi, |
| 58 | + "getExperiments" | "getAxiosInstance" |
| 59 | + >, |
| 60 | + private readonly sessionState: SessionState, |
| 61 | + private readonly logger: Logger, |
| 62 | + ) { |
| 63 | + this.#disposables.push( |
| 64 | + this.sessionState.onDidChange((session) => { |
| 65 | + void this.detect(session); |
| 66 | + }), |
| 67 | + vscode.workspace.onDidChangeConfiguration((e) => { |
| 68 | + if (e.affectsConfiguration(ENABLED_SETTING)) { |
| 69 | + this.#onDidChange.fire(); |
| 70 | + } |
| 71 | + }), |
| 72 | + ); |
| 73 | + void this.detect(this.sessionState.current); |
| 74 | + } |
| 75 | + |
| 76 | + public provideMcpServerDefinitions(): vscode.McpHttpServerDefinition[] { |
| 77 | + const enabled = vscode.workspace |
| 78 | + .getConfiguration() |
| 79 | + .get<boolean>(ENABLED_SETTING, true); |
| 80 | + if (!enabled || !this.#serverUrl) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + return [ |
| 84 | + new vscode.McpHttpServerDefinition( |
| 85 | + "Coder", |
| 86 | + vscode.Uri.parse(this.#serverUrl), |
| 87 | + ), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + public dispose(): void { |
| 92 | + for (const disposable of this.#disposables) { |
| 93 | + disposable.dispose(); |
| 94 | + } |
| 95 | + this.#onDidChange.dispose(); |
| 96 | + } |
| 97 | + |
| 98 | + private async detect(session: SessionData): Promise<void> { |
| 99 | + if (session.kind !== "signedIn") { |
| 100 | + this.setServerUrl(undefined); |
| 101 | + return; |
| 102 | + } |
| 103 | + const serverUrl = `${removeTrailingSlashes(session.deployment.url)}${MCP_ENDPOINT}`; |
| 104 | + if (this.#serverUrl !== serverUrl) { |
| 105 | + this.setServerUrl(undefined); |
| 106 | + } |
| 107 | + const available = await this.isServerAvailable(); |
| 108 | + // A sign-out or deployment switch during the checks supersedes them. |
| 109 | + if (this.sessionState.current === session) { |
| 110 | + this.setServerUrl(available ? serverUrl : undefined); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The endpoint authenticates before checking its gates, so an |
| 116 | + * unauthenticated probe returns 401 even when disabled. Check the gates |
| 117 | + * directly: the `mcp-server-http` experiment and the OAuth2 provider, whose |
| 118 | + * resource metadata VS Code needs to sign in. |
| 119 | + */ |
| 120 | + private async isServerAvailable(): Promise<boolean> { |
| 121 | + try { |
| 122 | + const experiments = await this.client.getExperiments(); |
| 123 | + if (!experiments.includes("mcp-server-http")) { |
| 124 | + this.logger.debug("Coder MCP server unavailable: experiment disabled"); |
| 125 | + return false; |
| 126 | + } |
| 127 | + await this.client |
| 128 | + .getAxiosInstance() |
| 129 | + .get(`/.well-known/oauth-protected-resource${MCP_ENDPOINT}`); |
| 130 | + this.logger.info("Coder MCP server available"); |
| 131 | + return true; |
| 132 | + } catch (error) { |
| 133 | + this.logger.debug("Coder MCP server unavailable", error); |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private setServerUrl(url: string | undefined): void { |
| 139 | + if (this.#serverUrl === url) { |
| 140 | + return; |
| 141 | + } |
| 142 | + this.#serverUrl = url; |
| 143 | + this.#onDidChange.fire(); |
| 144 | + } |
| 145 | +} |
0 commit comments