@contextos/mcp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@contextos/mcp",
3
+ "version": "0.1.0",
4
+ "description": "Model Context Protocol server for ContextOS - enables AI tools to access optimized context",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "bin": {
8
+ "contextos-mcp": "./dist/index.js"
9
+ },
10
+ "scripts": {
11
+ "build": "tsup src/index.ts --format esm --clean",
12
+ "dev": "tsx watch src/index.ts",
13
+ "start": "node dist/index.js"
14
+ },
15
+ "dependencies": {
16
+ "@modelcontextprotocol/sdk": "^0.5.0",
17
+ "@contextos/core": "workspace:*"
18
+ },
19
+ "devDependencies": {
20
+ "tsup": "^8.0.0",
21
+ "tsx": "^4.7.0",
22
+ "typescript": "^5.4.0"
23
+ },
24
+ "keywords": [
25
+ "mcp",
26
+ "model-context-protocol",
27
+ "ai",
28
+ "context",
29
+ "cursor",
30
+ "claude"
31
+ ],
32
+ "author": "ContextOS Team",
33
+ "license": "MIT"
34
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * MCP Tool, Resource, and Prompt Definitions
3
+ * Defines all capabilities exposed by the ContextOS MCP server
4
+ */
5
+
6
+ import type { Tool, Resource, Prompt } from '@modelcontextprotocol/sdk/types.js';
7
+
8
+ // ═══════════════════════════════════════════════════════════
9
+ // TOOLS - Actions that AI can perform
10
+ // ═══════════════════════════════════════════════════════════
11
+
12
+ export const TOOLS: Tool[] = [
13
+ {
14
+ name: 'contextos_build',
15
+ description: 'Build optimized context for a specific goal. Returns the most relevant files from the codebase based on semantic similarity, dependency graph, and custom rules.',
16
+ inputSchema: {
17
+ type: 'object',
18
+ properties: {
19
+ goal: {
20
+ type: 'string',
21
+ description: 'The coding task or goal (e.g., "Add authentication to UserController")',
22
+ },
23
+ },
24
+ required: ['goal'],
25
+ },
26
+ },
27
+ {
28
+ name: 'contextos_analyze',
29
+ description: 'Perform deep analysis of the codebase using RLM engine. Can find patterns, security issues, or answer complex questions about the code.',
30
+ inputSchema: {
31
+ type: 'object',
32
+ properties: {
33
+ query: {
34
+ type: 'string',
35
+ description: 'Analysis query (e.g., "Find potential security vulnerabilities")',
36
+ },
37
+ },
38
+ required: ['query'],
39
+ },
40
+ },
41
+ {
42
+ name: 'contextos_find',
43
+ description: 'Find files matching a pattern in the indexed codebase.',
44
+ inputSchema: {
45
+ type: 'object',
46
+ properties: {
47
+ pattern: {
48
+ type: 'string',
49
+ description: 'Glob pattern to match (e.g., "**/auth/**/*.ts")',
50
+ },
51
+ },
52
+ required: ['pattern'],
53
+ },
54
+ },
55
+ {
56
+ name: 'contextos_deps',
57
+ description: 'Get dependencies of a file up to a specified depth.',
58
+ inputSchema: {
59
+ type: 'object',
60
+ properties: {
61
+ file: {
62
+ type: 'string',
63
+ description: 'File path to analyze dependencies for',
64
+ },
65
+ depth: {
66
+ type: 'number',
67
+ description: 'Maximum depth to traverse (default: 2)',
68
+ },
69
+ },
70
+ required: ['file'],
71
+ },
72
+ },
73
+ {
74
+ name: 'contextos_explain',
75
+ description: 'Get an AI-powered explanation of a file, including its purpose, key functions, and how it relates to other parts of the codebase.',
76
+ inputSchema: {
77
+ type: 'object',
78
+ properties: {
79
+ file: {
80
+ type: 'string',
81
+ description: 'File path to explain',
82
+ },
83
+ },
84
+ required: ['file'],
85
+ },
86
+ },
87
+ {
88
+ name: 'contextos_status',
89
+ description: 'Get the current status of ContextOS: project info, index status, and configuration.',
90
+ inputSchema: {
91
+ type: 'object',
92
+ properties: {},
93
+ },
94
+ },
95
+ ];
96
+
97
+ // ═══════════════════════════════════════════════════════════
98
+ // RESOURCES - Data that AI can read
99
+ // ═══════════════════════════════════════════════════════════
100
+
101
+ export const RESOURCES: Resource[] = [
102
+ {
103
+ uri: 'contextos://context/current',
104
+ name: 'Current Context',
105
+ description: 'The most recently built context (output of ctx build/goal)',
106
+ mimeType: 'text/markdown',
107
+ },
108
+ {
109
+ uri: 'contextos://project/info',
110
+ name: 'Project Info',
111
+ description: 'Project configuration from context.yaml (name, language, framework, stack)',
112
+ mimeType: 'application/json',
113
+ },
114
+ {
115
+ uri: 'contextos://project/constraints',
116
+ name: 'Coding Constraints',
117
+ description: 'Project coding rules and constraints that should be followed',
118
+ mimeType: 'text/markdown',
119
+ },
120
+ {
121
+ uri: 'contextos://project/structure',
122
+ name: 'Project Structure',
123
+ description: 'Directory tree of the project (excluding node_modules, etc.)',
124
+ mimeType: 'text/plain',
125
+ },
126
+ ];
127
+
128
+ // ═══════════════════════════════════════════════════════════
129
+ // PROMPTS - Pre-built prompt templates
130
+ // ═══════════════════════════════════════════════════════════
131
+
132
+ export const PROMPTS: Prompt[] = [
133
+ {
134
+ name: 'code_with_context',
135
+ description: 'Start a coding task with optimized context from ContextOS',
136
+ arguments: [
137
+ {
138
+ name: 'goal',
139
+ description: 'The coding task or goal',
140
+ required: true,
141
+ },
142
+ ],
143
+ },
144
+ {
145
+ name: 'review_code',
146
+ description: 'Review a file and its dependencies',
147
+ arguments: [
148
+ {
149
+ name: 'file',
150
+ description: 'File path to review',
151
+ required: true,
152
+ },
153
+ ],
154
+ },
155
+ {
156
+ name: 'debug_issue',
157
+ description: 'Debug an issue with relevant context',
158
+ arguments: [
159
+ {
160
+ name: 'issue',
161
+ description: 'Description of the issue',
162
+ required: true,
163
+ },
164
+ ],
165
+ },
166
+ ];
package/src/index.ts ADDED
@@ -0,0 +1,253 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ContextOS MCP Server
4
+ *
5
+ * Model Context Protocol server that provides AI tools (Claude Code, Cursor, etc.)
6
+ * with optimized context from ContextOS.
7
+ *
8
+ * Usage:
9
+ * npx @contextos/mcp
10
+ *
11
+ * Configure in Claude Desktop:
12
+ * {
13
+ * "mcpServers": {
14
+ * "contextos": {
15
+ * "command": "npx",
16
+ * "args": ["@contextos/mcp"]
17
+ * }
18
+ * }
19
+ * }
20
+ */
21
+
22
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
23
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
24
+ import {
25
+ CallToolRequestSchema,
26
+ ListToolsRequestSchema,
27
+ ListResourcesRequestSchema,
28
+ ReadResourceRequestSchema,
29
+ ListPromptsRequestSchema,
30
+ GetPromptRequestSchema,
31
+ } from '@modelcontextprotocol/sdk/types.js';
32
+
33
+ import { ContextOSProvider } from './provider.js';
34
+ import { TOOLS, RESOURCES, PROMPTS } from './definitions.js';
35
+
36
+ // Initialize server
37
+ const server = new Server(
38
+ {
39
+ name: 'contextos',
40
+ version: '0.1.0',
41
+ },
42
+ {
43
+ capabilities: {
44
+ tools: {},
45
+ resources: {},
46
+ prompts: {},
47
+ },
48
+ }
49
+ );
50
+
51
+ // Initialize ContextOS provider
52
+ const provider = new ContextOSProvider();
53
+
54
+ // ═══════════════════════════════════════════════════════════
55
+ // TOOLS - Actions that AI can perform
56
+ // ═══════════════════════════════════════════════════════════
57
+
58
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
59
+ tools: TOOLS,
60
+ }));
61
+
62
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
63
+ const { name, arguments: args } = request.params;
64
+
65
+ try {
66
+ switch (name) {
67
+ case 'contextos_build': {
68
+ const goal = (args as { goal: string }).goal;
69
+ const result = await provider.buildContext(goal);
70
+ return {
71
+ content: [{ type: 'text', text: result }],
72
+ };
73
+ }
74
+
75
+ case 'contextos_analyze': {
76
+ const query = (args as { query: string }).query;
77
+ const result = await provider.analyze(query);
78
+ return {
79
+ content: [{ type: 'text', text: result }],
80
+ };
81
+ }
82
+
83
+ case 'contextos_find': {
84
+ const pattern = (args as { pattern: string }).pattern;
85
+ const result = await provider.findFiles(pattern);
86
+ return {
87
+ content: [{ type: 'text', text: result }],
88
+ };
89
+ }
90
+
91
+ case 'contextos_deps': {
92
+ const file = (args as { file: string }).file;
93
+ const depth = (args as { file: string; depth?: number }).depth || 2;
94
+ const result = await provider.getDependencies(file, depth);
95
+ return {
96
+ content: [{ type: 'text', text: result }],
97
+ };
98
+ }
99
+
100
+ case 'contextos_explain': {
101
+ const file = (args as { file: string }).file;
102
+ const result = await provider.explainFile(file);
103
+ return {
104
+ content: [{ type: 'text', text: result }],
105
+ };
106
+ }
107
+
108
+ case 'contextos_status': {
109
+ const result = await provider.getStatus();
110
+ return {
111
+ content: [{ type: 'text', text: result }],
112
+ };
113
+ }
114
+
115
+ default:
116
+ throw new Error(`Unknown tool: ${name}`);
117
+ }
118
+ } catch (error) {
119
+ return {
120
+ content: [{
121
+ type: 'text',
122
+ text: `Error: ${error instanceof Error ? error.message : String(error)}`
123
+ }],
124
+ isError: true,
125
+ };
126
+ }
127
+ });
128
+
129
+ // ═══════════════════════════════════════════════════════════
130
+ // RESOURCES - Data that AI can read
131
+ // ═══════════════════════════════════════════════════════════
132
+
133
+ server.setRequestHandler(ListResourcesRequestSchema, async () => ({
134
+ resources: RESOURCES,
135
+ }));
136
+
137
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
138
+ const { uri } = request.params;
139
+
140
+ try {
141
+ if (uri === 'contextos://context/current') {
142
+ const content = await provider.getCurrentContext();
143
+ return {
144
+ contents: [{ uri, mimeType: 'text/markdown', text: content }],
145
+ };
146
+ }
147
+
148
+ if (uri === 'contextos://project/info') {
149
+ const content = await provider.getProjectInfo();
150
+ return {
151
+ contents: [{ uri, mimeType: 'application/json', text: content }],
152
+ };
153
+ }
154
+
155
+ if (uri === 'contextos://project/constraints') {
156
+ const content = await provider.getConstraints();
157
+ return {
158
+ contents: [{ uri, mimeType: 'text/markdown', text: content }],
159
+ };
160
+ }
161
+
162
+ if (uri === 'contextos://project/structure') {
163
+ const content = await provider.getProjectStructure();
164
+ return {
165
+ contents: [{ uri, mimeType: 'text/plain', text: content }],
166
+ };
167
+ }
168
+
169
+ throw new Error(`Unknown resource: ${uri}`);
170
+ } catch (error) {
171
+ throw new Error(`Failed to read resource: ${error instanceof Error ? error.message : String(error)}`);
172
+ }
173
+ });
174
+
175
+ // ═══════════════════════════════════════════════════════════
176
+ // PROMPTS - Pre-built prompt templates
177
+ // ═══════════════════════════════════════════════════════════
178
+
179
+ server.setRequestHandler(ListPromptsRequestSchema, async () => ({
180
+ prompts: PROMPTS,
181
+ }));
182
+
183
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
184
+ const { name, arguments: args } = request.params;
185
+
186
+ switch (name) {
187
+ case 'code_with_context': {
188
+ const goal = args?.goal || 'general coding task';
189
+ const context = await provider.buildContext(goal);
190
+ return {
191
+ messages: [
192
+ {
193
+ role: 'user',
194
+ content: {
195
+ type: 'text',
196
+ text: `# Project Context\n\n${context}\n\n# Task\n\n${goal}`,
197
+ },
198
+ },
199
+ ],
200
+ };
201
+ }
202
+
203
+ case 'review_code': {
204
+ const file = args?.file || '';
205
+ const content = await provider.getFileWithDeps(file);
206
+ return {
207
+ messages: [
208
+ {
209
+ role: 'user',
210
+ content: {
211
+ type: 'text',
212
+ text: `# Code Review Request\n\nPlease review the following code and its dependencies:\n\n${content}`,
213
+ },
214
+ },
215
+ ],
216
+ };
217
+ }
218
+
219
+ case 'debug_issue': {
220
+ const issue = args?.issue || 'unknown issue';
221
+ const context = await provider.buildContext(`debug: ${issue}`);
222
+ return {
223
+ messages: [
224
+ {
225
+ role: 'user',
226
+ content: {
227
+ type: 'text',
228
+ text: `# Debug Request\n\n## Issue\n${issue}\n\n## Relevant Context\n\n${context}`,
229
+ },
230
+ },
231
+ ],
232
+ };
233
+ }
234
+
235
+ default:
236
+ throw new Error(`Unknown prompt: ${name}`);
237
+ }
238
+ });
239
+
240
+ // ═══════════════════════════════════════════════════════════
241
+ // START SERVER
242
+ // ═══════════════════════════════════════════════════════════
243
+
244
+ async function main() {
245
+ const transport = new StdioServerTransport();
246
+ await server.connect(transport);
247
+ console.error('ContextOS MCP Server started');
248
+ }
249
+
250
+ main().catch((error) => {
251
+ console.error('Failed to start server:', error);
252
+ process.exit(1);
253
+ });