Integrate MCP resource mentions into CodeMirror editors
CodeMirror extension implementing MCP for text editors - autocomplete and click-handling for @resource mentions and /prompt commands.
Why it matters
Enable developers to add Model Context Protocol support to their CodeMirror-based code editors, allowing users to reference external resources and invoke prompt commands directly within the editing environment.
Outcomes
What it gets done
Parse and recognize MCP resource mention syntax in editor content
Handle prompt command invocations from within the code editor
Provide structured output for MCP resource references
Extend CodeMirror with MCP protocol capabilities
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/mcp-codemirror-mcp | bash Overview
Codemirror Mcp
A CodeMirror extension implementing MCP resource mentions and prompt commands directly in the editor: @resource autocomplete with clickable, styled decorations, and /prompt command autocomplete, connected to an MCP server over a configurable transport. Use it when building a CodeMirror-based chat or prompt editor that should let users reference MCP resources and invoke MCP prompts inline, with the referenced resources extractable to attach as context to a submitted prompt.
What it does
codemirror-mcp is a CodeMirror extension that brings MCP (Model Context Protocol) resource mentions and prompt commands directly into a text editor. Typing @ triggers autocomplete for MCP resources, which then render as visually styled, clickable decorations with hover tooltips; typing / triggers autocomplete for MCP prompt commands. A helper function extracts every resource referenced in the current editor content, formatted with its URI, type, and description, ready to be appended to a prompt before it's sent to an AI server.
When to use - and when NOT to
Use it when building a chat, prompt, or notebook-style editor on top of CodeMirror that should let a user reference MCP resources inline while typing, rather than through a separate picker UI, and that should invoke MCP prompt commands with the same kind of familiar slash-command autocomplete used elsewhere. It is a CodeMirror 6 extension specifically, requiring @codemirror/view and @codemirror/state as peer dependencies alongside the official MCP SDK, so it fits editors already built on that stack rather than other editor frameworks.
Inputs and outputs
Input is an MCP transport, for example a WebSocketClientTransport pointed at a running MCP server, plus optional client options such as name and version and a resource-click handler passed to mcpExtension() when constructing the CodeMirror EditorView. Output is the live editor behavior: resource and prompt autocomplete, clickable resource decorations, and an extractResources(view) call that returns the resources referenced in the current document, which a submit handler can format and append to the outgoing prompt text.
Integrations
Built directly on the official @modelcontextprotocol/sdk for the underlying MCP client and transport, the example uses WebSocket transport, and on @codemirror/view and @codemirror/state as the editor foundation. Published as @marimo-team/codemirror-mcp on npm, installable with npm or pnpm.
Who it's for
Developers who want that resource-mention and prompt-command autocomplete without building the decoration and popup UI themselves. The project is MIT licensed.
Source README
codemirror-mcp
A CodeMirror extension that implements the Model Context Protocol (MCP) for resource mentions and prompt commands.
Features
- Resource Completion: Autocomplete for
@resourcementions - Resource Decorations: Visual styling for
@resourcementions with click handling - Prompt Completion: Autocomplete for
/promptcommands - Theme Support: Customizable styling
Installation
npm install @marimo-team/codemirror-mcp @modelcontextprotocol/sdk
# or
pnpm add @marimo-team/codemirror-mcp @modelcontextprotocol/sdk
Peer Dependencies
This module requires the following peer dependencies:
@codemirror/view@codemirror/state@modelcontextprotocol/sdk
Usage
import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js";
import { mcpExtension, extractResources } from '@marimo-team/codemirror-mcp';
import { EditorView } from '@codemirror/view';
const transport = new WebSocketClientTransport(new URL('ws://localhost:8080'));
const view = new EditorView({
extensions: [
// ... other extensions
mcpExtension({
// Required options
transport: transport,
// Optional options
logger: console,
clientOptions: {
name: 'your-client',
version: '1.0.0'
},
onResourceClick: (resource) => {
// Open resource
// e.g. open in a tab, etc.
},
}),
// Handle submit
keymap.of([
{
key: 'Enter',
run: () => {
const resources = extractResources(view);
const formattedResources = resources
.map(
({ resource }) =>
`${resource.uri} (${resource.type}): ${resource.description || resource.name}`
)
.join('\n');
const prompt = `${view.state.doc.toString()}\n\nResources:\n${formattedResources}`;
// ... submit prompt to AI server
// const response = await generateText(prompt);
},
},
]),
],
parent: document.querySelector('#editor'),
});
Resources
- Use
@resource-urisyntax to reference resources - Resources are visually decorated and clickable
- Click handling for resource interactions
- Hover tooltips show resource details
- Customizable theme
Prompts
- Use
/commandsyntax for prompt commands - Autocomplete for available prompts
Development
# Install dependencies
pnpm install
# Run tests
pnpm test
# Run demo
pnpm dev
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.