-
Notifications
You must be signed in to change notification settings - Fork 5.1k
API guide: register MCP server #8260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ntrogh
wants to merge
8
commits into
main
Choose a base branch
from
register-mcp-server-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25cb99b
Add MCP api guide
ntrogh e8f5069
Update related content
ntrogh 584b8df
Merge branch 'main' into register-mcp-server-api
ntrogh ea1fb6c
Update with latest API changes
ntrogh 2380add
Update api/extension-guides/mcp.md
ntrogh 55f8c7a
Add details on package.json configuration
ntrogh ea6a4f4
Merge branch 'register-mcp-server-api' of https://github.com/microsof…
ntrogh 6bd4545
Add MCP server definition resolver
ntrogh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
# DO NOT TOUCH — Managed by doc writer | ||
ContentId: e655f324-ed0b-452d-aff3-52cdca3978a5 | ||
DateApproved: 05/08/2025 | ||
|
||
# Summarize the whole topic in less than 300 characters for SEO purpose | ||
MetaDescription: A guide to registering an MCP server in a VS Code extension. | ||
--- | ||
|
||
# MCP servers | ||
|
||
Model Context Protocol (MCP) is an open standard that enables AI models to interact with external tools and services through a unified interface. Visual Studio Code can act as an MCP client, which enables users to [access MCP tools in agent mode](/docs/copilot/chat/mcp-servers.md). This article guides you through registering an MCP server in a VS Code extension. | ||
|
||
VS Code retrieves MCP server configurations from an `.vscode/mcp.json` file in the workspace, user settings, or can automatically discover them from other tools like Claude Desktop. | ||
|
||
VS Code extensions can also register MCP server configurations programmatically to avoid that users need to manually configure them. This is useful if you already have an MCP server and want to register it as part of your extension activation, or if your extension has a dependency on an MCP server. | ||
|
||
Instead of using MCP servers to extend the chat functionality, you can also [contribute language model tools](/api/extension-guides/tools.md) directly within your extension. This approach is useful if you want to deeply integrate with VS Code by using extension APIs or to avoid that users have to install and run an MCP server in a separate process. | ||
|
||
> [!IMPORTANT] | ||
> MCP support in VS Code is in preview and the API for registering an MCP server in a VS Code extension is currently in a proposed state. | ||
|
||
## Register an MCP server | ||
|
||
To register an MCP server in your extension, use the `vscode.lm.registerMcpServerDefinitionProvider` API to provide the [MCP configuration](/docs/copilot/chat/mcp-servers.md#configuration-format) for the server. The API takes a `providerId` string and a `McpServerDefinitionProvider` object. | ||
ntrogh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `McpServerDefinitionProvider` object has two properties: | ||
|
||
- `onDidChangeMcpServerDefinitions`: event that is triggered when the MCP server configurations change. | ||
- `provideMcpServerDefinitions`: function that returns an array of MCP server configurations (`vscode.McpServerDefinition[]`). | ||
ntrogh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An `McpServerDefinition` object can be one of the following types: | ||
|
||
- `vscode.McpStdioServerDefinition`: represents an MCP server available by running a local process and operating on its stdin and stdout streams. | ||
- `vscode.McpHttpServerDefinition`: represents an MCP server available using the Streamable HTTP transport. | ||
|
||
The following example demonstrates how to register an MCP server in an extension. | ||
|
||
```ts | ||
import * as vscode from 'vscode'; | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
const didChangeEmitter = new vscode.EventEmitter<void>(); | ||
|
||
context.subscriptions.push(vscode.lm.registerMcpServerDefinitionProvider('exampleGist', { | ||
onDidChangeMcpServerDefinitions: didChangeEmitter.event, | ||
provideMcpServerDefinitions: async () => { | ||
let servers: vscode.McpServerDefinition[] = []; | ||
|
||
let apiKey = vscode.workspace.getConfiguration().get<string>('apikey') || ''; | ||
|
||
// Example of a simple stdio server definition | ||
servers.push(new vscode.McpStdioServerDefinition( | ||
{ | ||
label: 'my-server', | ||
command: 'node', | ||
args: ['server.js'], | ||
cwd: vscode.Uri.file('/path/to/server'), | ||
env: { | ||
API_KEY: apiKey | ||
}, | ||
version: '1.0.0' | ||
}); | ||
|
||
// Example of an HTTP server definition | ||
servers.push(new vscode.McpHttpServerDefinition( | ||
{ | ||
label: 'my-http-server', | ||
uri: 'http://localhost:3000', | ||
headers: { | ||
Authorization: `Bearer ${apiKey}` | ||
}, | ||
version: '1.0.0' | ||
})); | ||
|
||
return servers; | ||
} | ||
})); | ||
} | ||
``` | ||
|
||
## Getting started | ||
|
||
Get started with a full example of how to register an MCP server in a VS Code extension: | ||
|
||
- [MCP extension sample](https://github.com/microsoft/vscode-extension-samples/blob/main/mcp-extension-sample) | ||
|
||
## Related content | ||
|
||
- [Model Context Protocol Documentation](https://modelcontextprotocol.io/) | ||
- [Use MCP tools in agent mode](/docs/copilot/chat/mcp-servers.md) | ||
- [Contribute a language model tool](/api/extension-guides/tools.md) | ||
- [Language Model API reference](/api/references/vscode-api.md#lm) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.