Skip to content

Commit 8839447

Browse files
committed
feat(mcp): offer the deployment's MCP server to chat and the Agents window
1 parent 361252f commit 8839447

8 files changed

Lines changed: 422 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
- `Coder: Open Workspace in Agents Window` opens a workspace in the VS Code
1414
Agents window, starting it first if needed. It opens the template's
1515
directory, or the folder you last opened on the workspace.
16+
- Chat and the Agents window can use the Coder deployment's MCP server when
17+
the deployment enables the `mcp-server-http` experiment and the OAuth2
18+
provider. Sign in to the "Coder" server from the MCP Servers view. Turn it
19+
off with `coder.mcpServer.enabled`.
20+
21+
### Changed
22+
23+
- The extension activates when the editor starts, so its MCP server is
24+
available in the Agents window. The "Remote SSH extension not found"
25+
warning is shown once instead of on every activation.
1626

1727
## [v1.18.4](https://github.com/coder/vscode-coder/releases/tag/v1.18.4) 2026-12-03
1828

‎package.json‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@
183183
"type": "boolean",
184184
"default": false
185185
},
186+
"coder.mcpServer.enabled": {
187+
"markdownDescription": "Offer the Coder deployment's MCP server to chat and the Agents window. Only deployments with the `mcp-server-http` experiment and the OAuth2 provider enabled serve it. Sign in to it from the MCP Servers view.",
188+
"type": "boolean",
189+
"default": true,
190+
"scope": "application"
191+
},
186192
"coder.disableSignatureVerification": {
187193
"markdownDescription": "Disable Coder CLI signature verification, which can be useful if you run an unsigned fork of the binary.",
188194
"type": "boolean",
@@ -330,6 +336,12 @@
330336
}
331337
}
332338
},
339+
"mcpServerDefinitionProviders": [
340+
{
341+
"id": "coder",
342+
"label": "Coder"
343+
}
344+
],
333345
"viewsContainers": {
334346
"activitybar": [
335347
{
@@ -841,7 +853,8 @@
841853
"activationEvents": [
842854
"onResolveRemoteAuthority:ssh-remote",
843855
"onCommand:coder.connect",
844-
"onUri"
856+
"onUri",
857+
"onStartupFinished"
845858
],
846859
"dependencies": {
847860
"@abraham/reflection": "^0.14.0",

‎src/extension.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ActivationTelemetry,
2424
type ActivationTracer,
2525
} from "./instrumentation/activation";
26+
import { registerMcpServerProvider } from "./mcp/mcpServerProvider";
2627
import { OAuthSessionManager } from "./oauth/sessionManager";
2728
import { ConnectionSetup } from "./remote/connectionSetup";
2829
import { Remote } from "./remote/remote";
@@ -38,6 +39,9 @@ import { WorkspaceProvider } from "./workspace/workspacesProvider";
3839

3940
import type { WorkspaceFilter } from "@repo/shared";
4041

42+
import type { Logger } from "./logging/logger";
43+
import type { MementoManager } from "./storage/mementoManager";
44+
4145
const MY_WORKSPACES_TREE_ID = "myWorkspaces";
4246
const SHARED_WORKSPACES_TREE_ID = "sharedWorkspaces";
4347
const ALL_WORKSPACES_TREE_ID = "allWorkspaces";
@@ -70,6 +74,8 @@ async function doActivate(
7074
// Means that vscodium is not supported by this for now
7175

7276
const remoteSshExtension = getRemoteSshExtension();
77+
const output = serviceContainer.getLogger();
78+
const mementoManager = serviceContainer.getMementoManager();
7379

7480
let vscodeProposed: typeof vscode = vscode;
7581

@@ -80,18 +86,12 @@ async function doActivate(
8086
vscodeProposed = extensionRequire("vscode") as typeof vscode;
8187
} else if (!isAgentsWindow()) {
8288
// The Agents window disables Remote SSH, so its absence there is expected.
83-
vscode.window.showErrorMessage(
84-
"Remote SSH extension not found, this may not work as expected.\n" +
85-
// NB should we link to documentation or marketplace?
86-
"Please install your choice of Remote SSH extension from the VS Code Marketplace.",
87-
);
89+
await warnRemoteSshMissing(mementoManager, output);
8890
}
8991

9092
// Initialize the global vscodeProposed module for use throughout the extension
9193
initVscodeProposed(vscodeProposed);
9294

93-
const output = serviceContainer.getLogger();
94-
const mementoManager = serviceContainer.getMementoManager();
9595
const secretsManager = serviceContainer.getSecretsManager();
9696
const contextManager = serviceContainer.getContextManager();
9797
const commandManager = serviceContainer.getCommandManager();
@@ -180,6 +180,15 @@ async function doActivate(
180180
);
181181
ctx.subscriptions.push(announcementManager);
182182

183+
const mcpServerRegistration = registerMcpServerProvider(
184+
client,
185+
deploymentManager.session,
186+
output,
187+
);
188+
if (mcpServerRegistration) {
189+
ctx.subscriptions.push(mcpServerRegistration);
190+
}
191+
183192
// The deprecated Tasks panel only shows on deployments old enough to
184193
// still support it, which requires fetching the deployment version.
185194
const updateTasksSupported = async () => {
@@ -594,6 +603,26 @@ async function doActivate(
594603
}
595604
}
596605

606+
/**
607+
* Shows the missing Remote SSH warning once per install and logs it after
608+
* that, since the extension activates at startup in every window.
609+
*/
610+
async function warnRemoteSshMissing(
611+
mementoManager: MementoManager,
612+
logger: Logger,
613+
): Promise<void> {
614+
logger.warn("Remote SSH extension not found");
615+
if (mementoManager.hasShownRemoteSshMissing()) {
616+
return;
617+
}
618+
await mementoManager.setShownRemoteSshMissing();
619+
void vscode.window.showErrorMessage(
620+
"Remote SSH extension not found, this may not work as expected.\n" +
621+
// NB should we link to documentation or marketplace?
622+
"Please install your choice of Remote SSH extension from the VS Code Marketplace.",
623+
);
624+
}
625+
597626
/**
598627
* Migrates old flat storage (sessionToken) to new label-based map storage.
599628
* This is a one-time operation that runs on extension activation.

‎src/mcp/mcpServerProvider.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}

‎src/storage/mementoManager.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const MAX_URLS = 10;
1010
const DEPLOYMENT_ACCESS_PREFIX = "coder.access.";
1111
const SURFACED_BANNERS_PREFIX = "coder.surfacedBanners.";
1212
const DISMISSED_SSH_OPTIONS_PREFIX = "coder.dismissedSshOptions.";
13+
const REMOTE_SSH_MISSING_SHOWN_KEY = "coder.remoteSshMissingShown";
1314

1415
const KeysSchema = z.array(z.string());
1516

@@ -108,6 +109,15 @@ export class MementoManager {
108109
await this.memento.update("url", undefined);
109110
}
110111

112+
/** Whether the missing Remote SSH extension warning was already shown. */
113+
public hasShownRemoteSshMissing(): boolean {
114+
return this.memento.get<boolean>(REMOTE_SSH_MISSING_SHOWN_KEY) === true;
115+
}
116+
117+
public async setShownRemoteSshMissing(): Promise<void> {
118+
await this.memento.update(REMOTE_SSH_MISSING_SHOWN_KEY, true);
119+
}
120+
111121
public getSurfacedBanners(safeHostname: string): string[] {
112122
return this.getKeys(SURFACED_BANNERS_PREFIX + safeHostname);
113123
}

‎test/mocks/vscode.runtime.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ export const extensions = {
308308
all: [] as unknown[],
309309
};
310310

311+
export class McpHttpServerDefinition {
312+
constructor(
313+
public readonly label: string,
314+
public readonly uri: Uri,
315+
) {}
316+
}
317+
318+
export const lm = {
319+
registerMcpServerDefinitionProvider: vi.fn(() => ({ dispose: vi.fn() })),
320+
};
321+
311322
export const version = "1.106.0-test";
312323

313324
const vscode = {
@@ -331,6 +342,8 @@ const vscode = {
331342
workspace,
332343
env,
333344
extensions,
345+
McpHttpServerDefinition,
346+
lm,
334347
version,
335348
};
336349

‎test/scopes/probes.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const apiProbes = {
2121
getBuildInfo: ({ scoped }) => scoped.getBuildInfo(),
2222
getAuthenticatedUser: ({ scoped }) => scoped.getAuthenticatedUser(),
2323
getAppearance: ({ scoped }) => scoped.getAppearance(),
24+
getExperiments: ({ scoped }) => scoped.getExperiments(),
2425
getDeploymentSSHConfig: ({ scoped }) => scoped.getDeploymentSSHConfig(),
2526
getWorkspaces: async ({ scoped }) =>
2627
assertNonEmpty((await scoped.getWorkspaces({ q: "owner:me" })).workspaces),

0 commit comments

Comments
 (0)