|
1 | 1 | # IDE Auto-Completion Server |
2 | 2 |
|
3 | | -A **real-world MCP completion server** that provides intelligent auto-completion suggestions for developers working in IDEs and code editors. This example demonstrates how to build production-ready completion functionality by loading data from external JSON files and providing context-aware suggestions. |
| 3 | +Demonstrates the **real MCP completion protocol** — `completion/complete` — |
| 4 | +served by an `McpCompletion` provider registered with |
| 5 | +`.completion_provider()`, alongside a plain tool for contrast. |
4 | 6 |
|
5 | | -## Real-World Use Case |
| 7 | +Two different surfaces: |
6 | 8 |
|
7 | | -This server simulates an **IDE language server or editor plugin** that helps developers with: |
| 9 | +| Surface | Method | For | |
| 10 | +|---|---|---| |
| 11 | +| Completion provider | `completion/complete` | Argument autocomplete while a user edits a prompt/template (IDE-style) | |
| 12 | +| Tool | `tools/call` (`ide_completion`) | Model-invoked suggestion lookups | |
8 | 13 |
|
9 | | -- **🔤 Code completion** for programming languages and frameworks |
10 | | -- **⚙️ Command suggestions** for development tools and operations |
11 | | -- **📁 File path completion** for common project files |
12 | | -- **🌍 Environment completion** for deployment contexts and configurations |
13 | | -- **📦 Tool recommendations** based on project context |
| 14 | +The provider completes the `language` argument of the `code_review` prompt: |
| 15 | +the routing handler matches the request's `ref` against the provider's |
| 16 | +declared reference, prefix-filters, and the framework enforces the spec's |
| 17 | +100-item response cap. |
14 | 18 |
|
15 | | -### Why External Data Files? |
16 | | - |
17 | | -Unlike hardcoded completion data, this server loads suggestions from **external JSON files** demonstrating: |
18 | | -- **Maintainability**: Update completion data without code changes |
19 | | -- **Customization**: Different teams can maintain their own completion databases |
20 | | -- **Scalability**: Easy to add new languages, frameworks, and tools |
21 | | -- **Real-world pattern**: How production completion servers manage their data |
22 | | - |
23 | | -## Architecture |
24 | | - |
25 | | -``` |
26 | | -completion-server/ |
27 | | -├── src/main.rs # Server implementation |
28 | | -├── data/ # External completion data |
29 | | -│ ├── languages.json # Programming languages with categories |
30 | | -│ ├── frameworks.json # Web frameworks with language mappings |
31 | | -│ └── development_commands.json # Commands with tool examples |
32 | | -└── README.md |
33 | | -``` |
34 | | - |
35 | | -## Features |
36 | | - |
37 | | -### 🎯 **Context-Aware Completion** |
38 | | -- Suggests different completions based on parameter names |
39 | | -- Filters suggestions by current input prefix |
40 | | -- Provides rich descriptions with categories and examples |
41 | | - |
42 | | -### 📊 **Categorized Data** |
43 | | -- **Programming languages** by category (systems, web, functional, etc.) |
44 | | -- **Frameworks** by language and type (frontend, backend, fullstack) |
45 | | -- **Commands** by operation type (build, deploy, monitoring, etc.) |
46 | | - |
47 | | -### ⚡ **Production Ready** |
48 | | -- Loads data from external JSON files at startup |
49 | | -- Efficient prefix filtering and result limiting |
50 | | -- Comprehensive error handling and logging |
51 | | - |
52 | | -## Running the Server |
| 19 | +## Run |
53 | 20 |
|
54 | 21 | ```bash |
55 | | -# Ensure you're in the completion-server directory for data/ access |
56 | | -cd examples/completion-server |
57 | | - |
58 | | -# Run the IDE completion server (default: 127.0.0.1:8042) |
59 | 22 | cargo run -p completion-server |
60 | | - |
61 | | -# Test language completion |
62 | | -curl -X POST http://127.0.0.1:8042/mcp \ |
63 | | - -H "Content-Type: application/json" \ |
64 | | - -d '{ |
65 | | - "jsonrpc": "2.0", |
66 | | - "method": "completion/complete", |
67 | | - "params": { |
68 | | - "argument": { |
69 | | - "name": "language", |
70 | | - "value": "ru" |
71 | | - }, |
72 | | - "ref": {} |
73 | | - }, |
74 | | - "id": 1 |
75 | | - }' |
76 | | -``` |
77 | | - |
78 | | -## Completion Categories |
79 | | - |
80 | | -### 1. Programming Languages |
81 | | - |
82 | | -**Triggered by**: `language`, `lang`, `programming_language` |
83 | | - |
84 | | -Suggests programming languages with categories and detailed descriptions loaded from `data/languages.json`. |
85 | | - |
86 | | -**Example Response:** |
87 | | -```json |
88 | | -{ |
89 | | - "jsonrpc": "2.0", |
90 | | - "result": { |
91 | | - "completions": [ |
92 | | - { |
93 | | - "value": "rust", |
94 | | - "label": "Rust programming language", |
95 | | - "description": "Systems programming language focused on safety and performance (systems)" |
96 | | - } |
97 | | - ] |
98 | | - }, |
99 | | - "id": 1 |
100 | | -} |
101 | | -``` |
102 | | - |
103 | | -### 2. Web Frameworks |
104 | | - |
105 | | -**Triggered by**: `framework`, `library`, `lib` |
106 | | - |
107 | | -Suggests frameworks with language and category information from `data/frameworks.json`. |
108 | | - |
109 | | -**Example Request:** |
110 | | -```bash |
111 | | -curl -X POST http://127.0.0.1:8042/mcp \ |
112 | | - -H "Content-Type: application/json" \ |
113 | | - -d '{ |
114 | | - "jsonrpc": "2.0", |
115 | | - "method": "completion/complete", |
116 | | - "params": { |
117 | | - "argument": { |
118 | | - "name": "framework", |
119 | | - "value": "rea" |
120 | | - }, |
121 | | - "ref": {} |
122 | | - }, |
123 | | - "id": 1 |
124 | | - }' |
| 23 | +# → http://127.0.0.1:8042/mcp |
125 | 24 | ``` |
126 | 25 |
|
127 | | -**Example Response:** |
128 | | -```json |
129 | | -{ |
130 | | - "jsonrpc": "2.0", |
131 | | - "result": { |
132 | | - "completions": [ |
133 | | - { |
134 | | - "value": "react", |
135 | | - "label": "React framework", |
136 | | - "description": "JavaScript library for building user interfaces (javascript - frontend)" |
137 | | - } |
138 | | - ] |
139 | | - }, |
140 | | - "id": 1 |
141 | | -} |
142 | | -``` |
143 | | - |
144 | | -### 3. Development Commands |
145 | | - |
146 | | -**Triggered by**: `command`, `cmd`, `action` |
147 | | - |
148 | | -Suggests development commands with tool examples from `data/development_commands.json`. |
| 26 | +## Try it (2026-07-28 stateless) |
149 | 27 |
|
150 | | -**Example Request:** |
151 | 28 | ```bash |
152 | | -curl -X POST http://127.0.0.1:8042/mcp \ |
153 | | - -H "Content-Type: application/json" \ |
154 | | - -d '{ |
155 | | - "jsonrpc": "2.0", |
156 | | - "method": "completion/complete", |
157 | | - "params": { |
158 | | - "argument": { |
159 | | - "name": "command", |
160 | | - "value": "bu" |
161 | | - }, |
162 | | - "ref": {} |
163 | | - }, |
164 | | - "id": 1 |
165 | | - }' |
166 | | -``` |
167 | | - |
168 | | -**Example Response:** |
169 | | -```json |
170 | | -{ |
171 | | - "jsonrpc": "2.0", |
172 | | - "result": { |
173 | | - "completions": [ |
174 | | - { |
175 | | - "value": "build", |
176 | | - "label": "Build command", |
177 | | - "description": "Compile and build the project - Tools: cargo, npm, gradle, maven, make" |
178 | | - } |
179 | | - ] |
180 | | - }, |
181 | | - "id": 1 |
182 | | -} |
183 | | -``` |
184 | | - |
185 | | -### 4. File Extensions |
186 | | - |
187 | | -**Triggered by**: `extension`, `ext`, `file_extension` |
188 | | - |
189 | | -Suggests common file extensions with detailed descriptions for different file types. |
190 | | - |
191 | | -### 5. File Paths |
192 | | - |
193 | | -**Triggered by**: `filename`, `file`, `path` |
194 | | - |
195 | | -Suggests common project files like `main.rs`, `README.md`, `Cargo.toml`, `package.json`. |
196 | | - |
197 | | -### 6. Semantic Versions |
198 | | - |
199 | | -**Triggered by**: `version` |
200 | | - |
201 | | -Suggests semantic version patterns: `1.0.0`, `0.1.0`, `2.0.0-beta`. |
202 | | - |
203 | | -### 7. Deployment Environments |
204 | | - |
205 | | -**Triggered by**: `environment`, `env` |
206 | | - |
207 | | -Suggests environment types: `development`, `staging`, `production`. |
208 | | - |
209 | | -## External Data Format |
210 | | - |
211 | | -### Languages Data (`data/languages.json`) |
212 | | -```json |
213 | | -{ |
214 | | - "programming_languages": [ |
215 | | - { |
216 | | - "name": "rust", |
217 | | - "label": "Rust programming language", |
218 | | - "description": "Systems programming language focused on safety and performance", |
219 | | - "category": "systems" |
220 | | - } |
221 | | - ] |
222 | | -} |
223 | | -``` |
224 | | - |
225 | | -### Frameworks Data (`data/frameworks.json`) |
226 | | -```json |
227 | | -{ |
228 | | - "web_frameworks": [ |
229 | | - { |
230 | | - "name": "react", |
231 | | - "label": "React framework", |
232 | | - "description": "JavaScript library for building user interfaces", |
233 | | - "category": "frontend", |
234 | | - "language": "javascript" |
235 | | - } |
236 | | - ] |
237 | | -} |
238 | | -``` |
239 | | - |
240 | | -### Commands Data (`data/development_commands.json`) |
241 | | -```json |
242 | | -{ |
243 | | - "development_commands": [ |
244 | | - { |
245 | | - "name": "build", |
246 | | - "label": "Build command", |
247 | | - "description": "Compile and build the project", |
248 | | - "category": "compilation", |
249 | | - "common_tools": ["cargo", "npm", "gradle", "maven", "make"] |
250 | | - } |
251 | | - ] |
252 | | -} |
253 | | -``` |
254 | | - |
255 | | -## Implementation Highlights |
256 | | - |
257 | | -### Data Loading |
258 | | -```rust |
259 | | -impl IdeCompletionHandler { |
260 | | - pub fn new() -> Result<Self, Box<dyn std::error::Error>> { |
261 | | - let data_dir = Path::new("data"); |
262 | | - |
263 | | - let languages = Self::load_languages(data_dir)?; |
264 | | - let frameworks = Self::load_frameworks(data_dir)?; |
265 | | - let commands = Self::load_commands(data_dir)?; |
266 | | - |
267 | | - Ok(Self { languages, frameworks, commands, file_extensions }) |
268 | | - } |
269 | | -} |
270 | | -``` |
271 | | - |
272 | | -### Context-Aware Completion |
273 | | -```rust |
274 | | -fn get_smart_completions(&self, argument_name: &str, current_value: &str) -> Vec<CompletionSuggestion> { |
275 | | - match argument_name.to_lowercase().as_str() { |
276 | | - "language" | "lang" | "programming_language" => { |
277 | | - self.get_language_completions(&prefix) |
278 | | - }, |
279 | | - "framework" | "library" | "lib" => { |
280 | | - self.get_framework_completions(&prefix) |
281 | | - }, |
282 | | - "command" | "cmd" | "action" => { |
283 | | - self.get_command_completions(&prefix) |
284 | | - }, |
285 | | - _ => /* fallback suggestions */ |
286 | | - } |
287 | | -} |
288 | | -``` |
289 | | - |
290 | | -## Real-World Applications |
291 | | - |
292 | | -### IDE Integration |
293 | | -- **VS Code Language Server**: Provide completion for configuration files |
294 | | -- **IntelliJ Plugin**: Framework and library suggestions |
295 | | -- **Vim/Neovim LSP**: Command and tool completion |
296 | | - |
297 | | -### Development Tools |
298 | | -- **CLI Tools**: Smart completion for command-line applications |
299 | | -- **CI/CD Pipelines**: Environment and deployment target completion |
300 | | -- **Code Generators**: Template and framework selection |
301 | | - |
302 | | -### Configuration Management |
303 | | -- **Docker Compose**: Service and image completion |
304 | | -- **Kubernetes**: Resource and namespace completion |
305 | | -- **Terraform**: Provider and resource completion |
306 | | - |
307 | | -## Extension Opportunities |
308 | | - |
309 | | -### Enhanced Data Sources |
310 | | -- **Package Registries**: npm, crates.io, PyPI integration |
311 | | -- **Documentation APIs**: Real-time API completion |
312 | | -- **Git Repositories**: Branch and tag completion |
313 | | -- **Cloud Services**: AWS, GCP, Azure resource completion |
314 | | - |
315 | | -### Advanced Features |
316 | | -- **Fuzzy Matching**: More sophisticated search algorithms |
317 | | -- **Machine Learning**: AI-powered suggestions based on context |
318 | | -- **User Preferences**: Personalized completion based on usage history |
319 | | -- **Multi-workspace**: Different completion sets per project type |
320 | | - |
321 | | -### Performance Optimizations |
322 | | -- **Caching**: Cache frequently accessed completion data |
323 | | -- **Incremental Loading**: Load data on-demand for large datasets |
324 | | -- **Background Updates**: Refresh completion data without restarts |
325 | | - |
326 | | -This completion server demonstrates how external data files enable maintainable, scalable completion systems that can evolve with changing development ecosystems without requiring code changes. |
| 29 | +META='"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientInfo":{"name":"curl","version":"1.0"},"io.modelcontextprotocol/clientCapabilities":{}}' |
| 30 | + |
| 31 | +# Argument completion: "ru" → ["ruby","rust"] |
| 32 | +curl -s -X POST http://127.0.0.1:8042/mcp \ |
| 33 | + -H 'Content-Type: application/json' \ |
| 34 | + -H 'MCP-Protocol-Version: 2026-07-28' \ |
| 35 | + -H 'Mcp-Method: completion/complete' \ |
| 36 | + -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"completion/complete\",\"params\":{\"ref\":{\"type\":\"ref/prompt\",\"name\":\"code_review\"},\"argument\":{\"name\":\"language\",\"value\":\"ru\"},$META}}" |
| 37 | + |
| 38 | +# The prompt the completion serves |
| 39 | +curl -s -X POST http://127.0.0.1:8042/mcp \ |
| 40 | + -H 'Content-Type: application/json' \ |
| 41 | + -H 'MCP-Protocol-Version: 2026-07-28' \ |
| 42 | + -H 'Mcp-Method: prompts/get' -H 'Mcp-Name: code_review' \ |
| 43 | + -d "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"prompts/get\",\"params\":{\"name\":\"code_review\",\"arguments\":{\"language\":\"rust\"},$META}}" |
| 44 | + |
| 45 | +# The contrast tool |
| 46 | +curl -s -X POST http://127.0.0.1:8042/mcp \ |
| 47 | + -H 'Content-Type: application/json' \ |
| 48 | + -H 'MCP-Protocol-Version: 2026-07-28' \ |
| 49 | + -H 'Mcp-Method: tools/call' -H 'Mcp-Name: ide_completion' \ |
| 50 | + -d "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"tools/call\",\"params\":{\"name\":\"ide_completion\",\"arguments\":{\"category\":\"language\",\"prefix\":\"py\"},$META}}" |
| 51 | +``` |
| 52 | + |
| 53 | +## Contract notes |
| 54 | + |
| 55 | +- A server with completion providers advertises the `completions` |
| 56 | + capability in `server/discover`; one without answers |
| 57 | + `completion/complete` with 404 + `-32601`. |
| 58 | +- Malformed params (missing `argument`, unknown `ref` type) → `-32602`. |
| 59 | +- Provider output is capped at 100 values (`total`/`hasMore` reflect a cut). |
0 commit comments