Editor Context MCP Server - VS Code Extension
This VS Code extension exposes editor context (active file, selection, visible lines) to any MCP client (e.g. a coding agent) via an MCP (Model Context Protocol) server.
Features
- Active File: Full path of the currently open file
- Selection: Current text selection with line/column positions
- Visible Ranges: Currently visible line ranges in the editor
- Language ID: Programming language of the active file
- On-Demand Fetching: Returns current editor state when requested
- Notebook Support: Supports Jupyter notebooks with cell selection and visible ranges
Architecture
┌────────────────────────────┐ ┌──────────────────────────┐
│ VS Code Extension │ │ A coding agent │
│ (editor context) │ │ (e.g. opencode) │
│ ┌──────────────────────┐ │ │ ┌────────────────────┐ │
│ │ MCP Server │ │ │ │ MCP Client │ │
│ │ (mcp-server.ts) │ │ │ │ │ │
│ │ │──┼── MCP API ───┼─►│ • File path │ │
│ │ • HTTP server │ │ │ │ • Language ID │ │
│ │ • getEditorContext │ │ │ │ • Selected text │ │
│ │ • /mcp endpoint │ │ │ │ • Lines range │ │
│ └──────────────────────┘ │ │ └────────────────────┘ │
└────────────────────────────┘ └──────────────────────────┘
Prerequisites
- VS Code 1.85.0+
- NVM (Node Version Manager)
- An MCP client (e.g., opencode CLI)
Build & Install
1. Setup Build Environment
cd vscode-context-mcp
nvm install 20
npm install
This compiles TypeScript to dist/ folder.
2. Build and Package the Extension (optional)
To create an installable .vsix file:
bash build.sh
This creates VSIX estension package.
3. Install the Extension
From built .vsix file:
bash install.sh
From source (development):
code --extension-development-host .
4. Open VS Code
The extension auto-activates when VS Code starts. You should see output in the Developer Tools console:
VSCode Context MCP server started on port <port>
VSCode Context MCP: MCP server running at http://127.0.0.1:<port>
opencode
Add the MCP server to your opencode config (opencode.json):
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"vscode-context-mcp": {
"type": "remote",
"url": "http://127.0.0.1:9876/mcp"
}
}
}
Other MCP Clients
Configure your MCP client to connect to http://127.0.0.1:9876.
Configuration
Port (default: 9876)
Change via VS Code Settings:
{
"vscodeContextMcp.port": 9876
}
Lock File Location
The extension writes connection info to:
~/.local/share/vscode-context-mcp/ide/<port>.lock
MCP Protocol
The extension implements the following MCP methods:
initialize - Initialize MCP session
tools/list - List available tools
tools/call - Call a tool
resources/list - List available resources
resources/read - Read editor://context resource
Tool: getEditorContext
Returns an EditorContext object with type and context:
- type:
"editor" | "notebook" | null
- context:
EditorState | NotebookState | null
Input Schema:
{}
Output - Text Editor:
{
"type": "editor",
"context": {
"activeFile": "/path/to/file.ts",
"selection": {
"startLine": 10,
"startColumn": 1,
"endLine": 20,
"endColumn": 5,
"text": "selected text..."
},
"visibleRanges": [
{ "startLine": 1, "startColumn": 1, "endLine": 50, "endColumn": 1 }
],
"languageId": "typescript"
}
}
Output - Notebook:
{
"type": "notebook",
"context": {
"notebookUri": "/path/to/notebook.ipynb",
"notebookType": "jupyter-notebook",
"selection": {
"startLine": 10,
"startColumn": 1,
"endLine": 20,
"endColumn": 5,
"text": "selected text..."
},
"cellRange": {
"startCell": 1,
"endCell": 3
},
"visibleRanges": [
{ "startCell": 1, "endCell": 10 }
],
"cellCount": 5,
"languageId": "python"
}
}
Output - No Active Editor:
{
"type": null,
"context": null
}
Resource: editor://context
Returns the same data as getEditorContext in JSON format.
Troubleshooting
Extension not starting?
- Check VS Code Developer Tools (Help > Toggle Developer Tools) for logs
- Ensure the extension is installed:
code --list-extensions
Client can't connect?
- Verify the MCP server is running in VS Code output
- Check the port matches in both VS Code settings and your client config
- Check for port conflicts:
lsof -i :9876
Port conflicts?
- Change the port in VS Code settings:
"vscodeContextMcp.port": 9877
License
MIT