Skip to content

Commit 7ba7eb9

Browse files
ntroghconnor4312
andauthored
API guide: register MCP server (#8260)
* Add MCP api guide * Update related content * Update with latest API changes * Update api/extension-guides/mcp.md Co-authored-by: Connor Peet <connor@peet.io> * Add details on package.json configuration * Add MCP server definition resolver * Apply minor edit after review * Add cross-linking and use AI wording --------- Co-authored-by: Connor Peet <connor@peet.io>
1 parent e5083e3 commit 7ba7eb9

6 files changed

Lines changed: 154 additions & 18 deletions

File tree

api/extension-guides/mcp.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
# DO NOT TOUCH — Managed by doc writer
3+
ContentId: e655f324-ed0b-452d-aff3-52cdca3978a5
4+
DateApproved: 05/08/2025
5+
6+
# Summarize the whole topic in less than 300 characters for SEO purpose
7+
MetaDescription: A guide to registering an MCP server in a VS Code extension.
8+
---
9+
10+
# MCP servers
11+
12+
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.
13+
14+
VS Code retrieves MCP server configurations from `.vscode/mcp.json` files in workspace folders and the `mcp` section of workspace, remote, and user settings. It can also automatically discover them from other tools' configuration, including Claude Desktop.
15+
16+
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.
17+
18+
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.
19+
20+
> [!IMPORTANT]
21+
> 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.
22+
23+
## Register an MCP server
24+
25+
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.
26+
27+
Before calling this method, extensions must contribute the `contributes.mcpServerDefinitionProviders` extension point in the `package.json` with the `id` of the provider.
28+
29+
The `McpServerDefinitionProvider` object has three properties:
30+
31+
- `onDidChangeMcpServerDefinitions`: event that is triggered when the MCP server configurations change.
32+
- `provideMcpServerDefinitions`: function that returns an array of MCP server configurations (`vscode.McpServerDefinition[]`).
33+
- `resolveMcpServerDefinition`: function that the editor calls when the MCP server needs to be started. Use this function to perform additional actions that may require user interaction, such as authentication.
34+
35+
An `McpServerDefinition` object can be one of the following types:
36+
37+
- `vscode.McpStdioServerDefinition`: represents an MCP server available by running a local process and operating on its stdin and stdout streams.
38+
- `vscode.McpHttpServerDefinition`: represents an MCP server available using the Streamable HTTP transport.
39+
40+
The following example demonstrates how to register MCP servers in an extension and prompt the user for an API key when starting the server.
41+
42+
```ts
43+
import * as vscode from 'vscode';
44+
45+
export function activate(context: vscode.ExtensionContext) {
46+
const didChangeEmitter = new vscode.EventEmitter<void>();
47+
48+
context.subscriptions.push(vscode.lm.registerMcpServerDefinitionProvider('exampleProvider', {
49+
onDidChangeMcpServerDefinitions: didChangeEmitter.event,
50+
provideMcpServerDefinitions: async () => {
51+
let servers: vscode.McpServerDefinition[] = [];
52+
53+
// Example of a simple stdio server definition
54+
servers.push(new vscode.McpStdioServerDefinition(
55+
{
56+
label: 'myServer',
57+
command: 'node',
58+
args: ['server.js'],
59+
cwd: vscode.Uri.file('/path/to/server'),
60+
env: {
61+
API_KEY: ''
62+
},
63+
version: '1.0.0'
64+
});
65+
66+
// Example of an HTTP server definition
67+
servers.push(new vscode.McpHttpServerDefinition(
68+
{
69+
label: 'myRemoteServer',
70+
uri: 'http://localhost:3000',
71+
headers: {
72+
'API_VERSION': '1.0.0'
73+
},
74+
version: '1.0.0'
75+
}));
76+
77+
return servers;
78+
},
79+
resolveMcpServerDefinition: async (server: vscode.McpServerDefinition) => {
80+
81+
if (server.label === 'myServer') {
82+
// Get the API key from the user, e.g. using vscode.window.showInputBox
83+
// Update the server definition with the API key
84+
}
85+
86+
// Return undefined to indicate that the server should not be started or throw an error
87+
// If there is a pending tool call, the editor will cancel it and return an error message
88+
// to the language model.
89+
return server;
90+
}
91+
}));
92+
}
93+
```
94+
95+
The `package.json` file should include the corresponding `contributes/mcpServerDefinitionProviders` section:
96+
97+
```json
98+
{
99+
...
100+
"contributes": {
101+
"mcpServerDefinitionProviders": [
102+
{
103+
"id": "exampleProvider",
104+
"label": "Example MCP Server Provider"
105+
}
106+
]
107+
}
108+
...
109+
}
110+
```
111+
112+
## Getting started
113+
114+
Get started with a full example of how to register an MCP server in a VS Code extension:
115+
116+
- [MCP extension sample](https://github.com/microsoft/vscode-extension-samples/blob/main/mcp-extension-sample)
117+
118+
## Related content
119+
120+
- [Model Context Protocol Documentation](https://modelcontextprotocol.io/)
121+
- [Use MCP tools in agent mode](/docs/copilot/chat/mcp-servers.md)
122+
- [Contribute a language model tool](/api/extension-guides/tools.md)
123+
- [Language Model API reference](/api/references/vscode-api.md#lm)

api/extension-guides/tools.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DateApproved: 05/08/2025
77
MetaDescription: A guide to creating a language model tool and how to implement tool calling in a chat extension
88
---
99

10-
# LanguageModelTool API
10+
# Language Model Tool API
1111

1212
Language model tools enable you to extend the functionality of a large language model (LLM). VS Code surfaces tools contributed by extensions in Copilot [agent mode](/docs/copilot/chat/chat-agent-mode.md). By contributing a tool in a VS Code extension, you can combine the power of agentic coding with deep VS Code integration via its extension APIs.
1313

@@ -267,6 +267,6 @@ Get more best practices for creating tools in the [OpenAI documentation](https:/
267267

268268
## Related content
269269

270-
- [Get started with the Language Model API](/api/extension-guides/language-model)
271-
- [Use Prompt-tsx](/api/extension-guides/prompt-tsx)
272-
- [Add MCP servers to chat](/docs/copilot/chat/mcp-servers)
270+
- [Language Model API reference](/api/references/vscode-api.md#lm)
271+
- [Register an MCP server in a VS Code extension](/api/extension-guides/mcp.md)
272+
- [Use MCP tools in agent mode](/docs/copilot/chat/mcp-servers.md)

api/toc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
["Language Model", "/api/extension-guides/language-model"],
3333
["Language Model Tutorial", "/api/extension-guides/language-model-tutorial"],
3434
["Language Model Tools", "/api/extension-guides/tools"],
35+
["MCP", "/api/extension-guides/mcp"],
3536
["Prompt TSX", "/api/extension-guides/prompt-tsx"],
3637
["Tree View", "/api/extension-guides/tree-view"],
3738
["Webview", "/api/extension-guides/webview"],

build/sitemap.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,16 @@
14741474
<changefreq>weekly</changefreq>
14751475
<priority>0.8</priority>
14761476
</url>
1477+
<url>
1478+
<loc>https://code.visualstudio.com/api/extension-guides/tools</loc>
1479+
<changefreq>weekly</changefreq>
1480+
<priority>0.8</priority>
1481+
</url>
1482+
<url>
1483+
<loc>https://code.visualstudio.com/api/extension-guides/mcp</loc>
1484+
<changefreq>weekly</changefreq>
1485+
<priority>0.8</priority>
1486+
</url>
14771487
<url>
14781488
<loc>https://code.visualstudio.com/api/extension-guides/prompt-tsx</loc>
14791489
<changefreq>weekly</changefreq>

docs/copilot/copilot-extensibility-overview.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
---
22
ContentId: e375ec2a-43d3-4670-96e5-fd25a6aed272
33
DateApproved: 05/08/2025
4-
MetaDescription: Overview of how to extend GitHub Copilot in your Visual Studio Code extension by using the Chat API or Language Model API.
4+
MetaDescription: Overview of how to extend the AI features in your Visual Studio Code extension by using the Chat API or Language Model API.
55
MetaSocialImage: images/shared/github-copilot-social.png
66
---
7-
# GitHub Copilot extensibility in VS Code
7+
# AI extensibility in VS Code
88

9-
Visual Studio Code has many AI features powered by GitHub Copilot to improve your coding experience, such as code completions, or natural language chat. You can further extend the built-in capabilities of Copilot, for example by contributing tools for [agent mode](/docs/copilot/chat/chat-agent-mode.md), or adding AI-powered features to your VS Code extension.
9+
Visual Studio Code has many AI features to improve your coding experience, such as code completions, or natural language chat. You can further extend the built-in capabilities, for example by contributing tools for [agent mode](/docs/copilot/chat/chat-agent-mode.md), or adding AI-powered features to your VS Code extension.
1010

11-
Depending on your use case, you have the following options for extending Copilot in your VS Code extension:
11+
Depending on your use case, you have the following options for extending AI in your VS Code extension:
1212

1313
- **Agent mode tool**: use the [Language Model Tool API](/api/extension-guides/tools.md) to contribute a tool for [agent mode](/docs/copilot/chat/chat-agent-mode.md) that is invoked automatically based on the user's prompt. Integrate deeply in VS Code by using other extension APIs in your tool.
1414

15-
- **MCP tool**: automatically register external [MCP tools](/docs/copilot/chat/mcp-servers.md) that can then be used in [agent mode](/docs/copilot/chat/chat-agent-mode.md). MCP tools run outside of the VS Code extension host and don't have access to the VS Code extension APIs.
15+
- **MCP tool**: automatically register external [MCP tools](/docs/copilot/chat/mcp-servers.md) that can then be used in [agent mode](/docs/copilot/chat/chat-agent-mode.md). As an extension developer, you can [register an MCP tool](/api/extension-guides/mcp.md) as part of your extension. MCP tools run outside of the VS Code extension host and don't have access to the VS Code extension APIs.
1616

1717
- **Chat participant**: use the [Chat](/api/extension-guides/chat.md) and [Language Model](/api/extension-guides/language-model.md) APIs to create a chat participant for [ask mode](/docs/copilot/chat/chat-ask-mode.md) that enables users to ask domain-specific questions by using natural language.
1818

19-
- **Use Copilot's LLM**: use the [Language Model API](/api/extension-guides/language-model.md) and the [VS Code extension APIs](/api/extension-guides/overview.md) to build custom AI-powered features into your extension and enhance editor-specific interactions.
19+
- **Use AI model**: use the [Language Model API](/api/extension-guides/language-model.md) and the [VS Code extension APIs](/api/extension-guides/overview.md) to build custom AI-powered features into your extension and enhance editor-specific interactions.
2020

21-
Alternatively, you can also build a **Copilot Extension**, implemented as a GitHub App with additional capabilities. Copilot Extensions work across all supported IDEs and GitHub, but don't have access to functionalities specific to VS Code. Get more info about [Copilot Extensions](https://docs.github.com/en/copilot/building-copilot-extensions/about-building-copilot-extensions) in the GitHub documentation.
21+
Alternatively, you can also build a GitHub Copilot Extension, implemented as a GitHub App with additional capabilities. Copilot Extensions work across all supported IDEs and GitHub, but don't have access to functionalities specific to VS Code. Get more info about [Copilot Extensions](https://docs.github.com/en/copilot/building-copilot-extensions/about-building-copilot-extensions) in the GitHub documentation.
2222

2323
## Use cases
2424

25-
You can use Copilot's capabilities to enhance the development experience in VS Code by integrating AI-powered features into your extension. Here are some examples of how you can use Copilot in your VS Code extension:
25+
Here are some examples of how you can use AI in your VS Code extension:
2626

2727
- **Docs querying**: use Retrieval-Augmented Generation (RAG) to query a third-party documentation service and generate responses based on the retrieved information.
2828

29-
- **AI-assisted coding**: use the Copilot LLM to provide editor annotations to provide coding suggestions.
29+
- **AI-assisted coding**: use the AI model to provide editor annotations to provide coding suggestions.
3030

31-
- **AI-powered reviews**: use the Copilot LLM to review your code for security vulnerabilities or performance improvements.
31+
- **AI-powered reviews**: use the AI model to review your code for security vulnerabilities or performance improvements.
3232

3333
- **Data retrieval**: query a database or third-party data service to retrieve information about a specific topic.
3434

3535
- **Enterprise coding assistant**: get chat responses that are grounded in the data of your enterprise and are aware of the specific coding guidelines your company follows.
3636

3737
- **Enhance extensions**: use the Language Model API to add AI-powered features to your existing VS Code extensions.
3838

39-
There are several examples already available in the Visual Studio Marketplace that extend Copilot in VS Code:
39+
There are several examples already available in the Visual Studio Marketplace that extend AI in VS Code:
4040

4141
- Agent mode tools: Go to the [Marketplace](https://marketplace.visualstudio.com/search?term=tag%3Alanguage-model-tools&target=VSCode&category=All%20categories&sortBy=Relevance) or search for the `language-model-tools` tag in the [Extensions view](/docs/getstarted/extensions.md).
4242

4343
- Chat participants: Go to the [Marketplace](https://marketplace.visualstudio.com/search?term=tag%3Achat-participant&target=VSCode&category=All%20categories&sortBy=Relevance) or search for the `chat-participant` tag in the [Extensions view](/docs/getstarted/extensions.md).
4444

45-
## Get started with Copilot extensibility in VS Code
45+
## Get started with AI extensibility in VS Code
4646

47-
To get started with extending Copilot in your VS Code extension, explore the following resources:
47+
To get started with extending AI in your VS Code extension, explore the following resources:
4848

4949
- [**Chat sample**](https://github.com/microsoft/vscode-extension-samples/tree/main/chat-sample): sample code for building a VS Code extension that contributes an agent mode tool and chat participant.
5050

51+
- [**MCP extension sample**](https://github.com/microsoft/vscode-extension-samples/blob/main/mcp-extension-sample): sample code for building a VS Code extension that registers an MCP tool.
52+
5153
- [**Tutorial: AI-powered code annotations**](/api/extension-guides/language-model-tutorial.md): step-by-step guide to implement a VS Code extension that uses the Language Model API to generate code annotations in the editor to help improve your code.
5254

5355
- [**Tutorial: Code tutor chat participant**](/api/extension-guides/chat-tutorial.md): step-by-step guide to implement a code tutor chat participant that enables users to ask for explaining a technical topic by using natural language in the Chat view in VS Code.

docs/toc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
}
153153
],
154154
["Tips and Tricks", "/docs/copilot/copilot-tips-and-tricks"],
155-
["Copilot Extensibility", "/docs/copilot/copilot-extensibility-overview"],
155+
["AI Extensibility", "/docs/copilot/copilot-extensibility-overview"],
156156
["FAQ", "/docs/copilot/faq"],
157157
["", "", {
158158
"name": "Reference",

0 commit comments

Comments
 (0)