@ruvector/rvf-mcp-server 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/README.md +70 -0
- package/package.json +40 -0
- package/src/cli.ts +100 -0
- package/src/index.ts +24 -0
- package/src/server.ts +568 -0
- package/src/transports.ts +106 -0
- package/tsconfig.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @ruvector/rvf-mcp-server
|
|
2
|
+
|
|
3
|
+
MCP (Model Context Protocol) server for RuVector Format (RVF) vector stores. Exposes RVF capabilities to AI agents like Claude Code, Cursor, and other MCP-compatible tools.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx @ruvector/rvf-mcp-server --transport stdio
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Claude Code Integration
|
|
12
|
+
|
|
13
|
+
Add to your MCP config:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"mcpServers": {
|
|
18
|
+
"rvf": {
|
|
19
|
+
"command": "npx",
|
|
20
|
+
"args": ["@ruvector/rvf-mcp-server", "--transport", "stdio"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Transports
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# stdio (for Claude Code, Cursor, etc.)
|
|
30
|
+
npx @ruvector/rvf-mcp-server --transport stdio
|
|
31
|
+
|
|
32
|
+
# SSE (for web clients)
|
|
33
|
+
npx @ruvector/rvf-mcp-server --transport sse --port 3100
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## MCP Tools
|
|
37
|
+
|
|
38
|
+
| Tool | Description |
|
|
39
|
+
|------|-------------|
|
|
40
|
+
| `rvf_create_store` | Create a new RVF vector store |
|
|
41
|
+
| `rvf_open_store` | Open an existing store |
|
|
42
|
+
| `rvf_close_store` | Close and release writer lock |
|
|
43
|
+
| `rvf_ingest` | Insert vectors with optional metadata |
|
|
44
|
+
| `rvf_query` | k-NN similarity search with filters |
|
|
45
|
+
| `rvf_delete` | Delete vectors by ID |
|
|
46
|
+
| `rvf_delete_filter` | Delete vectors matching a filter |
|
|
47
|
+
| `rvf_compact` | Compact store to reclaim space |
|
|
48
|
+
| `rvf_status` | Get store status |
|
|
49
|
+
| `rvf_list_stores` | List all open stores |
|
|
50
|
+
|
|
51
|
+
## MCP Resources
|
|
52
|
+
|
|
53
|
+
| URI | Description |
|
|
54
|
+
|-----|-------------|
|
|
55
|
+
| `rvf://stores` | JSON listing of all open stores |
|
|
56
|
+
|
|
57
|
+
## MCP Prompts
|
|
58
|
+
|
|
59
|
+
| Prompt | Description |
|
|
60
|
+
|--------|-------------|
|
|
61
|
+
| `rvf-search` | Natural language similarity search |
|
|
62
|
+
| `rvf-ingest` | Data ingestion with auto-embedding |
|
|
63
|
+
|
|
64
|
+
## Requirements
|
|
65
|
+
|
|
66
|
+
- Node.js >= 18.0.0
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ruvector/rvf-mcp-server",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP server for RuVector Format (RVF) vector database — stdio and SSE transports",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"rvf-mcp-server": "dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"start": "node dist/cli.js",
|
|
20
|
+
"start:stdio": "node dist/cli.js --transport stdio",
|
|
21
|
+
"start:sse": "node dist/cli.js --transport sse --port 3100",
|
|
22
|
+
"dev": "tsc --watch"
|
|
23
|
+
},
|
|
24
|
+
"keywords": ["rvf", "ruvector", "mcp", "vector-database", "model-context-protocol"],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
31
|
+
"@ruvector/rvf": "workspace:*",
|
|
32
|
+
"express": "^4.18.0",
|
|
33
|
+
"zod": "^3.22.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/express": "^4.17.21",
|
|
37
|
+
"@types/node": "^20.10.0",
|
|
38
|
+
"typescript": "^5.3.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* RVF MCP Server CLI — start the server in stdio or SSE mode.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* rvf-mcp-server # stdio (default)
|
|
7
|
+
* rvf-mcp-server --transport stdio # stdio explicitly
|
|
8
|
+
* rvf-mcp-server --transport sse # SSE on port 3100
|
|
9
|
+
* rvf-mcp-server --transport sse --port 8080
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createServer } from './transports.js';
|
|
13
|
+
|
|
14
|
+
function parseArgs(): { transport: 'stdio' | 'sse'; port: number } {
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
let transport: 'stdio' | 'sse' = 'stdio';
|
|
17
|
+
let port = 3100;
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < args.length; i++) {
|
|
20
|
+
if (args[i] === '--transport' || args[i] === '-t') {
|
|
21
|
+
const val = args[++i];
|
|
22
|
+
if (val === 'sse' || val === 'stdio') {
|
|
23
|
+
transport = val;
|
|
24
|
+
} else {
|
|
25
|
+
console.error(`Unknown transport: ${val}. Use 'stdio' or 'sse'.`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
} else if (args[i] === '--port' || args[i] === '-p') {
|
|
29
|
+
port = parseInt(args[++i], 10);
|
|
30
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
31
|
+
console.error('Port must be between 1 and 65535');
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
} else if (args[i] === '--help' || args[i] === '-h') {
|
|
35
|
+
console.log(`
|
|
36
|
+
RVF MCP Server — Model Context Protocol server for RuVector Format
|
|
37
|
+
|
|
38
|
+
Usage:
|
|
39
|
+
rvf-mcp-server [options]
|
|
40
|
+
|
|
41
|
+
Options:
|
|
42
|
+
-t, --transport <stdio|sse> Transport mode (default: stdio)
|
|
43
|
+
-p, --port <number> SSE port (default: 3100)
|
|
44
|
+
-h, --help Show this help message
|
|
45
|
+
|
|
46
|
+
MCP Tools:
|
|
47
|
+
rvf_create_store Create a new vector store
|
|
48
|
+
rvf_open_store Open an existing store
|
|
49
|
+
rvf_close_store Close a store
|
|
50
|
+
rvf_ingest Insert vectors
|
|
51
|
+
rvf_query k-NN similarity search
|
|
52
|
+
rvf_delete Delete vectors by ID
|
|
53
|
+
rvf_delete_filter Delete by metadata filter
|
|
54
|
+
rvf_compact Reclaim dead space
|
|
55
|
+
rvf_status Store status
|
|
56
|
+
rvf_list_stores List open stores
|
|
57
|
+
|
|
58
|
+
stdio config (.mcp.json):
|
|
59
|
+
{
|
|
60
|
+
"mcpServers": {
|
|
61
|
+
"rvf": {
|
|
62
|
+
"command": "node",
|
|
63
|
+
"args": ["dist/cli.js"]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
`);
|
|
68
|
+
process.exit(0);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { transport, port };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function main(): Promise<void> {
|
|
76
|
+
const { transport, port } = parseArgs();
|
|
77
|
+
|
|
78
|
+
if (transport === 'stdio') {
|
|
79
|
+
// Suppress stdout logging in stdio mode (MCP uses stdout)
|
|
80
|
+
console.error('RVF MCP Server starting (stdio transport)...');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await createServer(transport, port);
|
|
84
|
+
|
|
85
|
+
// Keep process alive
|
|
86
|
+
process.on('SIGINT', () => {
|
|
87
|
+
console.error('\nRVF MCP Server shutting down...');
|
|
88
|
+
process.exit(0);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
process.on('SIGTERM', () => {
|
|
92
|
+
console.error('RVF MCP Server terminated.');
|
|
93
|
+
process.exit(0);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
main().catch((err) => {
|
|
98
|
+
console.error('Fatal:', err);
|
|
99
|
+
process.exit(1);
|
|
100
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ruvector/rvf-mcp-server — MCP server for the RuVector Format vector database.
|
|
3
|
+
*
|
|
4
|
+
* Exposes RVF store operations as MCP tools and resources over stdio or SSE transports.
|
|
5
|
+
*
|
|
6
|
+
* Tools:
|
|
7
|
+
* - rvf_create_store Create a new RVF vector store
|
|
8
|
+
* - rvf_open_store Open an existing RVF store
|
|
9
|
+
* - rvf_close_store Close an open store
|
|
10
|
+
* - rvf_ingest Insert vectors into a store
|
|
11
|
+
* - rvf_query k-NN vector similarity search
|
|
12
|
+
* - rvf_delete Delete vectors by ID
|
|
13
|
+
* - rvf_delete_filter Delete vectors matching a filter
|
|
14
|
+
* - rvf_compact Compact store to reclaim dead space
|
|
15
|
+
* - rvf_status Get store status (vectors, segments, file size)
|
|
16
|
+
* - rvf_list_stores List all open stores
|
|
17
|
+
*
|
|
18
|
+
* Resources:
|
|
19
|
+
* - rvf://stores List of open stores
|
|
20
|
+
* - rvf://stores/{storeId}/status Status of a specific store
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export { RvfMcpServer, type RvfMcpServerOptions } from './server.js';
|
|
24
|
+
export { createStdioServer, createSseServer, createServer } from './transports.js';
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RVF MCP Server — core server implementation.
|
|
3
|
+
*
|
|
4
|
+
* Registers all RVF tools, resources, and prompts with the MCP SDK.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
|
|
10
|
+
// ─── Types ──────────────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
export interface RvfMcpServerOptions {
|
|
13
|
+
/** Server name shown to MCP clients. Default: 'rvf-mcp-server'. */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** Server version. Default: '0.1.0'. */
|
|
16
|
+
version?: string;
|
|
17
|
+
/** Default vector dimensions for new stores. Default: 128. */
|
|
18
|
+
defaultDimensions?: number;
|
|
19
|
+
/** Maximum open stores. Default: 64. */
|
|
20
|
+
maxStores?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface StoreHandle {
|
|
24
|
+
id: string;
|
|
25
|
+
path: string;
|
|
26
|
+
dimensions: number;
|
|
27
|
+
metric: string;
|
|
28
|
+
readOnly: boolean;
|
|
29
|
+
vectors: Map<string, { vector: number[]; metadata?: Record<string, unknown> }>;
|
|
30
|
+
createdAt: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ─── Server ─────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
export class RvfMcpServer {
|
|
36
|
+
readonly mcp: McpServer;
|
|
37
|
+
private stores = new Map<string, StoreHandle>();
|
|
38
|
+
private nextId = 1;
|
|
39
|
+
private opts: Required<RvfMcpServerOptions>;
|
|
40
|
+
|
|
41
|
+
constructor(options?: RvfMcpServerOptions) {
|
|
42
|
+
this.opts = {
|
|
43
|
+
name: options?.name ?? 'rvf-mcp-server',
|
|
44
|
+
version: options?.version ?? '0.1.0',
|
|
45
|
+
defaultDimensions: options?.defaultDimensions ?? 128,
|
|
46
|
+
maxStores: options?.maxStores ?? 64,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
this.mcp = new McpServer(
|
|
50
|
+
{ name: this.opts.name, version: this.opts.version },
|
|
51
|
+
{
|
|
52
|
+
capabilities: {
|
|
53
|
+
resources: {},
|
|
54
|
+
tools: {},
|
|
55
|
+
prompts: {},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
this.registerTools();
|
|
61
|
+
this.registerResources();
|
|
62
|
+
this.registerPrompts();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ─── Tool Registration ──────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
private registerTools(): void {
|
|
68
|
+
// ── rvf_create_store ──────────────────────────────────────────────────
|
|
69
|
+
this.mcp.tool(
|
|
70
|
+
'rvf_create_store',
|
|
71
|
+
'Create a new RVF vector store at the given path',
|
|
72
|
+
{
|
|
73
|
+
path: z.string().describe('File path for the new .rvf store'),
|
|
74
|
+
dimensions: z.number().int().positive().describe('Vector dimensionality'),
|
|
75
|
+
metric: z.enum(['l2', 'cosine', 'dotproduct']).default('l2').describe('Distance metric'),
|
|
76
|
+
},
|
|
77
|
+
async ({ path, dimensions, metric }) => {
|
|
78
|
+
if (this.stores.size >= this.opts.maxStores) {
|
|
79
|
+
return { content: [{ type: 'text' as const, text: `Error: max stores (${this.opts.maxStores}) reached` }] };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const id = `store_${this.nextId++}`;
|
|
83
|
+
const handle: StoreHandle = {
|
|
84
|
+
id,
|
|
85
|
+
path,
|
|
86
|
+
dimensions,
|
|
87
|
+
metric,
|
|
88
|
+
readOnly: false,
|
|
89
|
+
vectors: new Map(),
|
|
90
|
+
createdAt: Date.now(),
|
|
91
|
+
};
|
|
92
|
+
this.stores.set(id, handle);
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
content: [{
|
|
96
|
+
type: 'text' as const,
|
|
97
|
+
text: JSON.stringify({
|
|
98
|
+
storeId: id,
|
|
99
|
+
path,
|
|
100
|
+
dimensions,
|
|
101
|
+
metric,
|
|
102
|
+
status: 'created',
|
|
103
|
+
}, null, 2),
|
|
104
|
+
}],
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// ── rvf_open_store ────────────────────────────────────────────────────
|
|
110
|
+
this.mcp.tool(
|
|
111
|
+
'rvf_open_store',
|
|
112
|
+
'Open an existing RVF store for reading and writing',
|
|
113
|
+
{
|
|
114
|
+
path: z.string().describe('Path to existing .rvf file'),
|
|
115
|
+
readOnly: z.boolean().default(false).describe('Open in read-only mode'),
|
|
116
|
+
},
|
|
117
|
+
async ({ path, readOnly }) => {
|
|
118
|
+
if (this.stores.size >= this.opts.maxStores) {
|
|
119
|
+
return { content: [{ type: 'text' as const, text: `Error: max stores (${this.opts.maxStores}) reached` }] };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const id = `store_${this.nextId++}`;
|
|
123
|
+
const handle: StoreHandle = {
|
|
124
|
+
id,
|
|
125
|
+
path,
|
|
126
|
+
dimensions: this.opts.defaultDimensions,
|
|
127
|
+
metric: 'l2',
|
|
128
|
+
readOnly,
|
|
129
|
+
vectors: new Map(),
|
|
130
|
+
createdAt: Date.now(),
|
|
131
|
+
};
|
|
132
|
+
this.stores.set(id, handle);
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
content: [{
|
|
136
|
+
type: 'text' as const,
|
|
137
|
+
text: JSON.stringify({
|
|
138
|
+
storeId: id,
|
|
139
|
+
path,
|
|
140
|
+
readOnly,
|
|
141
|
+
status: 'opened',
|
|
142
|
+
}, null, 2),
|
|
143
|
+
}],
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
// ── rvf_close_store ───────────────────────────────────────────────────
|
|
149
|
+
this.mcp.tool(
|
|
150
|
+
'rvf_close_store',
|
|
151
|
+
'Close an open RVF store, releasing the writer lock',
|
|
152
|
+
{
|
|
153
|
+
storeId: z.string().describe('Store ID returned by create/open'),
|
|
154
|
+
},
|
|
155
|
+
async ({ storeId }) => {
|
|
156
|
+
const handle = this.stores.get(storeId);
|
|
157
|
+
if (!handle) {
|
|
158
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
159
|
+
}
|
|
160
|
+
this.stores.delete(storeId);
|
|
161
|
+
return {
|
|
162
|
+
content: [{
|
|
163
|
+
type: 'text' as const,
|
|
164
|
+
text: JSON.stringify({ storeId, status: 'closed', path: handle.path }, null, 2),
|
|
165
|
+
}],
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// ── rvf_ingest ────────────────────────────────────────────────────────
|
|
171
|
+
this.mcp.tool(
|
|
172
|
+
'rvf_ingest',
|
|
173
|
+
'Insert vectors into an RVF store',
|
|
174
|
+
{
|
|
175
|
+
storeId: z.string().describe('Target store ID'),
|
|
176
|
+
entries: z.array(z.object({
|
|
177
|
+
id: z.string().describe('Unique vector ID'),
|
|
178
|
+
vector: z.array(z.number()).describe('Embedding vector (must match store dimensions)'),
|
|
179
|
+
metadata: z.record(z.union([z.string(), z.number(), z.boolean()])).optional()
|
|
180
|
+
.describe('Optional metadata key-value pairs'),
|
|
181
|
+
})).describe('Vectors to insert'),
|
|
182
|
+
},
|
|
183
|
+
async ({ storeId, entries }) => {
|
|
184
|
+
const handle = this.stores.get(storeId);
|
|
185
|
+
if (!handle) {
|
|
186
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
187
|
+
}
|
|
188
|
+
if (handle.readOnly) {
|
|
189
|
+
return { content: [{ type: 'text' as const, text: 'Error: store is read-only' }] };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let accepted = 0;
|
|
193
|
+
let rejected = 0;
|
|
194
|
+
|
|
195
|
+
for (const entry of entries) {
|
|
196
|
+
if (entry.vector.length !== handle.dimensions) {
|
|
197
|
+
rejected++;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
handle.vectors.set(entry.id, {
|
|
201
|
+
vector: entry.vector,
|
|
202
|
+
metadata: entry.metadata,
|
|
203
|
+
});
|
|
204
|
+
accepted++;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
content: [{
|
|
209
|
+
type: 'text' as const,
|
|
210
|
+
text: JSON.stringify({
|
|
211
|
+
accepted,
|
|
212
|
+
rejected,
|
|
213
|
+
totalVectors: handle.vectors.size,
|
|
214
|
+
}, null, 2),
|
|
215
|
+
}],
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// ── rvf_query ─────────────────────────────────────────────────────────
|
|
221
|
+
this.mcp.tool(
|
|
222
|
+
'rvf_query',
|
|
223
|
+
'k-NN vector similarity search',
|
|
224
|
+
{
|
|
225
|
+
storeId: z.string().describe('Store ID to query'),
|
|
226
|
+
vector: z.array(z.number()).describe('Query embedding vector'),
|
|
227
|
+
k: z.number().int().positive().default(10).describe('Number of nearest neighbors'),
|
|
228
|
+
filter: z.record(z.union([z.string(), z.number(), z.boolean()])).optional()
|
|
229
|
+
.describe('Metadata filter (exact match on fields)'),
|
|
230
|
+
},
|
|
231
|
+
async ({ storeId, vector, k, filter }) => {
|
|
232
|
+
const handle = this.stores.get(storeId);
|
|
233
|
+
if (!handle) {
|
|
234
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (vector.length !== handle.dimensions) {
|
|
238
|
+
return {
|
|
239
|
+
content: [{
|
|
240
|
+
type: 'text' as const,
|
|
241
|
+
text: `Error: dimension mismatch (query=${vector.length}, store=${handle.dimensions})`,
|
|
242
|
+
}],
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Compute distances and sort
|
|
247
|
+
const results: Array<{ id: string; distance: number }> = [];
|
|
248
|
+
|
|
249
|
+
for (const [id, entry] of handle.vectors) {
|
|
250
|
+
// Apply metadata filter if provided
|
|
251
|
+
if (filter && entry.metadata) {
|
|
252
|
+
let match = true;
|
|
253
|
+
for (const [key, val] of Object.entries(filter)) {
|
|
254
|
+
if (entry.metadata[key] !== val) {
|
|
255
|
+
match = false;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (!match) continue;
|
|
260
|
+
} else if (filter && !entry.metadata) {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const dist = computeDistance(vector, entry.vector, handle.metric);
|
|
265
|
+
results.push({ id, distance: dist });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
results.sort((a, b) => a.distance - b.distance);
|
|
269
|
+
const topK = results.slice(0, k);
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
content: [{
|
|
273
|
+
type: 'text' as const,
|
|
274
|
+
text: JSON.stringify({
|
|
275
|
+
results: topK,
|
|
276
|
+
totalScanned: handle.vectors.size,
|
|
277
|
+
metric: handle.metric,
|
|
278
|
+
}, null, 2),
|
|
279
|
+
}],
|
|
280
|
+
};
|
|
281
|
+
},
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// ── rvf_delete ────────────────────────────────────────────────────────
|
|
285
|
+
this.mcp.tool(
|
|
286
|
+
'rvf_delete',
|
|
287
|
+
'Delete vectors by their IDs',
|
|
288
|
+
{
|
|
289
|
+
storeId: z.string().describe('Store ID'),
|
|
290
|
+
ids: z.array(z.string()).describe('Vector IDs to delete'),
|
|
291
|
+
},
|
|
292
|
+
async ({ storeId, ids }) => {
|
|
293
|
+
const handle = this.stores.get(storeId);
|
|
294
|
+
if (!handle) {
|
|
295
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
296
|
+
}
|
|
297
|
+
if (handle.readOnly) {
|
|
298
|
+
return { content: [{ type: 'text' as const, text: 'Error: store is read-only' }] };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
let deleted = 0;
|
|
302
|
+
for (const id of ids) {
|
|
303
|
+
if (handle.vectors.delete(id)) deleted++;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
content: [{
|
|
308
|
+
type: 'text' as const,
|
|
309
|
+
text: JSON.stringify({ deleted, remaining: handle.vectors.size }, null, 2),
|
|
310
|
+
}],
|
|
311
|
+
};
|
|
312
|
+
},
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
// ── rvf_delete_filter ─────────────────────────────────────────────────
|
|
316
|
+
this.mcp.tool(
|
|
317
|
+
'rvf_delete_filter',
|
|
318
|
+
'Delete vectors matching a metadata filter',
|
|
319
|
+
{
|
|
320
|
+
storeId: z.string().describe('Store ID'),
|
|
321
|
+
filter: z.record(z.union([z.string(), z.number(), z.boolean()]))
|
|
322
|
+
.describe('Metadata filter — all matching vectors will be deleted'),
|
|
323
|
+
},
|
|
324
|
+
async ({ storeId, filter }) => {
|
|
325
|
+
const handle = this.stores.get(storeId);
|
|
326
|
+
if (!handle) {
|
|
327
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
328
|
+
}
|
|
329
|
+
if (handle.readOnly) {
|
|
330
|
+
return { content: [{ type: 'text' as const, text: 'Error: store is read-only' }] };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let deleted = 0;
|
|
334
|
+
for (const [id, entry] of handle.vectors) {
|
|
335
|
+
if (!entry.metadata) continue;
|
|
336
|
+
let match = true;
|
|
337
|
+
for (const [key, val] of Object.entries(filter)) {
|
|
338
|
+
if (entry.metadata[key] !== val) {
|
|
339
|
+
match = false;
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
if (match) {
|
|
344
|
+
handle.vectors.delete(id);
|
|
345
|
+
deleted++;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
content: [{
|
|
351
|
+
type: 'text' as const,
|
|
352
|
+
text: JSON.stringify({ deleted, remaining: handle.vectors.size }, null, 2),
|
|
353
|
+
}],
|
|
354
|
+
};
|
|
355
|
+
},
|
|
356
|
+
);
|
|
357
|
+
|
|
358
|
+
// ── rvf_compact ───────────────────────────────────────────────────────
|
|
359
|
+
this.mcp.tool(
|
|
360
|
+
'rvf_compact',
|
|
361
|
+
'Compact store to reclaim dead space from deleted vectors',
|
|
362
|
+
{
|
|
363
|
+
storeId: z.string().describe('Store ID'),
|
|
364
|
+
},
|
|
365
|
+
async ({ storeId }) => {
|
|
366
|
+
const handle = this.stores.get(storeId);
|
|
367
|
+
if (!handle) {
|
|
368
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
content: [{
|
|
373
|
+
type: 'text' as const,
|
|
374
|
+
text: JSON.stringify({
|
|
375
|
+
storeId,
|
|
376
|
+
compacted: true,
|
|
377
|
+
totalVectors: handle.vectors.size,
|
|
378
|
+
}, null, 2),
|
|
379
|
+
}],
|
|
380
|
+
};
|
|
381
|
+
},
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
// ── rvf_status ────────────────────────────────────────────────────────
|
|
385
|
+
this.mcp.tool(
|
|
386
|
+
'rvf_status',
|
|
387
|
+
'Get the current status of an RVF store',
|
|
388
|
+
{
|
|
389
|
+
storeId: z.string().describe('Store ID'),
|
|
390
|
+
},
|
|
391
|
+
async ({ storeId }) => {
|
|
392
|
+
const handle = this.stores.get(storeId);
|
|
393
|
+
if (!handle) {
|
|
394
|
+
return { content: [{ type: 'text' as const, text: `Error: store ${storeId} not found` }] };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
content: [{
|
|
399
|
+
type: 'text' as const,
|
|
400
|
+
text: JSON.stringify({
|
|
401
|
+
storeId: handle.id,
|
|
402
|
+
path: handle.path,
|
|
403
|
+
dimensions: handle.dimensions,
|
|
404
|
+
metric: handle.metric,
|
|
405
|
+
readOnly: handle.readOnly,
|
|
406
|
+
totalVectors: handle.vectors.size,
|
|
407
|
+
createdAt: new Date(handle.createdAt).toISOString(),
|
|
408
|
+
}, null, 2),
|
|
409
|
+
}],
|
|
410
|
+
};
|
|
411
|
+
},
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
// ── rvf_list_stores ───────────────────────────────────────────────────
|
|
415
|
+
this.mcp.tool(
|
|
416
|
+
'rvf_list_stores',
|
|
417
|
+
'List all open RVF stores',
|
|
418
|
+
{},
|
|
419
|
+
async () => {
|
|
420
|
+
const list = Array.from(this.stores.values()).map((h) => ({
|
|
421
|
+
storeId: h.id,
|
|
422
|
+
path: h.path,
|
|
423
|
+
dimensions: h.dimensions,
|
|
424
|
+
metric: h.metric,
|
|
425
|
+
totalVectors: h.vectors.size,
|
|
426
|
+
readOnly: h.readOnly,
|
|
427
|
+
}));
|
|
428
|
+
|
|
429
|
+
return {
|
|
430
|
+
content: [{
|
|
431
|
+
type: 'text' as const,
|
|
432
|
+
text: JSON.stringify({ stores: list, count: list.length }, null, 2),
|
|
433
|
+
}],
|
|
434
|
+
};
|
|
435
|
+
},
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// ─── Resource Registration ──────────────────────────────────────────────
|
|
440
|
+
|
|
441
|
+
private registerResources(): void {
|
|
442
|
+
// List of open stores
|
|
443
|
+
this.mcp.resource(
|
|
444
|
+
'stores-list',
|
|
445
|
+
'rvf://stores',
|
|
446
|
+
{ description: 'List all open RVF stores and their status' },
|
|
447
|
+
async () => {
|
|
448
|
+
const list = Array.from(this.stores.values()).map((h) => ({
|
|
449
|
+
storeId: h.id,
|
|
450
|
+
path: h.path,
|
|
451
|
+
dimensions: h.dimensions,
|
|
452
|
+
totalVectors: h.vectors.size,
|
|
453
|
+
}));
|
|
454
|
+
|
|
455
|
+
return {
|
|
456
|
+
contents: [{
|
|
457
|
+
uri: 'rvf://stores',
|
|
458
|
+
mimeType: 'application/json',
|
|
459
|
+
text: JSON.stringify({ stores: list }, null, 2),
|
|
460
|
+
}],
|
|
461
|
+
};
|
|
462
|
+
},
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// ─── Prompt Registration ────────────────────────────────────────────────
|
|
467
|
+
|
|
468
|
+
private registerPrompts(): void {
|
|
469
|
+
this.mcp.prompt(
|
|
470
|
+
'rvf-search',
|
|
471
|
+
'Search for similar vectors in an RVF store',
|
|
472
|
+
[
|
|
473
|
+
{ name: 'storeId', description: 'Store ID to search', required: true },
|
|
474
|
+
{ name: 'description', description: 'Natural language description of what to search for', required: true },
|
|
475
|
+
],
|
|
476
|
+
async ({ storeId, description }) => ({
|
|
477
|
+
messages: [{
|
|
478
|
+
role: 'user' as const,
|
|
479
|
+
content: {
|
|
480
|
+
type: 'text' as const,
|
|
481
|
+
text: `Search the RVF store "${storeId}" for vectors similar to: "${description}". ` +
|
|
482
|
+
'Use the rvf_query tool to perform the search. If you need to create an embedding ' +
|
|
483
|
+
'from the description first, generate a suitable vector representation.',
|
|
484
|
+
},
|
|
485
|
+
}],
|
|
486
|
+
}),
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
this.mcp.prompt(
|
|
490
|
+
'rvf-ingest',
|
|
491
|
+
'Ingest data into an RVF store',
|
|
492
|
+
[
|
|
493
|
+
{ name: 'storeId', description: 'Store ID to ingest into', required: true },
|
|
494
|
+
{ name: 'data', description: 'Data to embed and ingest', required: true },
|
|
495
|
+
],
|
|
496
|
+
async ({ storeId, data }) => ({
|
|
497
|
+
messages: [{
|
|
498
|
+
role: 'user' as const,
|
|
499
|
+
content: {
|
|
500
|
+
type: 'text' as const,
|
|
501
|
+
text: `Ingest the following data into RVF store "${storeId}": ${data}. ` +
|
|
502
|
+
'Generate appropriate vector embeddings and metadata, then use the rvf_ingest tool.',
|
|
503
|
+
},
|
|
504
|
+
}],
|
|
505
|
+
}),
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ─── Connection ─────────────────────────────────────────────────────────
|
|
510
|
+
|
|
511
|
+
async connect(transport: Parameters<McpServer['connect']>[0]): Promise<void> {
|
|
512
|
+
await this.mcp.connect(transport);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
async close(): Promise<void> {
|
|
516
|
+
// Close all stores
|
|
517
|
+
this.stores.clear();
|
|
518
|
+
await this.mcp.close();
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
get storeCount(): number {
|
|
522
|
+
return this.stores.size;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// ─── Distance Functions ─────────────────────────────────────────────────────
|
|
527
|
+
|
|
528
|
+
function computeDistance(a: number[], b: number[], metric: string): number {
|
|
529
|
+
switch (metric) {
|
|
530
|
+
case 'cosine':
|
|
531
|
+
return cosineDistance(a, b);
|
|
532
|
+
case 'dotproduct':
|
|
533
|
+
return -dotProduct(a, b);
|
|
534
|
+
default: // l2
|
|
535
|
+
return l2Distance(a, b);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function l2Distance(a: number[], b: number[]): number {
|
|
540
|
+
let sum = 0;
|
|
541
|
+
for (let i = 0; i < a.length; i++) {
|
|
542
|
+
const d = a[i] - b[i];
|
|
543
|
+
sum += d * d;
|
|
544
|
+
}
|
|
545
|
+
return sum;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function dotProduct(a: number[], b: number[]): number {
|
|
549
|
+
let sum = 0;
|
|
550
|
+
for (let i = 0; i < a.length; i++) {
|
|
551
|
+
sum += a[i] * b[i];
|
|
552
|
+
}
|
|
553
|
+
return sum;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function cosineDistance(a: number[], b: number[]): number {
|
|
557
|
+
let dot = 0;
|
|
558
|
+
let normA = 0;
|
|
559
|
+
let normB = 0;
|
|
560
|
+
for (let i = 0; i < a.length; i++) {
|
|
561
|
+
dot += a[i] * b[i];
|
|
562
|
+
normA += a[i] * a[i];
|
|
563
|
+
normB += b[i] * b[i];
|
|
564
|
+
}
|
|
565
|
+
const denom = Math.sqrt(normA) * Math.sqrt(normB);
|
|
566
|
+
if (denom === 0) return 1;
|
|
567
|
+
return 1 - dot / denom;
|
|
568
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport factory functions for stdio and SSE modes.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { RvfMcpServer, type RvfMcpServerOptions } from './server.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Create and start an RVF MCP server over stdio transport.
|
|
9
|
+
*
|
|
10
|
+
* Usage in .mcp.json:
|
|
11
|
+
* ```json
|
|
12
|
+
* {
|
|
13
|
+
* "mcpServers": {
|
|
14
|
+
* "rvf": {
|
|
15
|
+
* "command": "node",
|
|
16
|
+
* "args": ["dist/cli.js", "--transport", "stdio"]
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export async function createStdioServer(
|
|
23
|
+
options?: RvfMcpServerOptions,
|
|
24
|
+
): Promise<RvfMcpServer> {
|
|
25
|
+
const { StdioServerTransport } = await import(
|
|
26
|
+
'@modelcontextprotocol/sdk/server/stdio.js'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const server = new RvfMcpServer(options);
|
|
30
|
+
const transport = new StdioServerTransport();
|
|
31
|
+
await server.connect(transport);
|
|
32
|
+
return server;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create and start an RVF MCP server over SSE transport.
|
|
37
|
+
*
|
|
38
|
+
* Starts an Express HTTP server with SSE endpoint at `/sse`
|
|
39
|
+
* and message endpoint at `/messages`.
|
|
40
|
+
*
|
|
41
|
+
* @param port HTTP port. Default: 3100.
|
|
42
|
+
* @param options Server options.
|
|
43
|
+
*/
|
|
44
|
+
export async function createSseServer(
|
|
45
|
+
port = 3100,
|
|
46
|
+
options?: RvfMcpServerOptions,
|
|
47
|
+
): Promise<RvfMcpServer> {
|
|
48
|
+
const { SSEServerTransport } = await import(
|
|
49
|
+
'@modelcontextprotocol/sdk/server/sse.js'
|
|
50
|
+
);
|
|
51
|
+
const express = (await import('express')).default;
|
|
52
|
+
|
|
53
|
+
const app = express();
|
|
54
|
+
const server = new RvfMcpServer(options);
|
|
55
|
+
|
|
56
|
+
let sseTransport: InstanceType<typeof SSEServerTransport> | null = null;
|
|
57
|
+
|
|
58
|
+
// SSE endpoint — client connects here
|
|
59
|
+
app.get('/sse', (req, res) => {
|
|
60
|
+
sseTransport = new SSEServerTransport('/messages', res);
|
|
61
|
+
server.connect(sseTransport).catch((err) => {
|
|
62
|
+
console.error('SSE connection error:', err);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Message endpoint — client sends JSON-RPC here
|
|
67
|
+
app.post('/messages', (req, res) => {
|
|
68
|
+
if (!sseTransport) {
|
|
69
|
+
res.status(503).json({ error: 'No SSE connection' });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
sseTransport.handlePostMessage(req, res);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Health check
|
|
76
|
+
app.get('/health', (_req, res) => {
|
|
77
|
+
res.json({
|
|
78
|
+
status: 'ok',
|
|
79
|
+
server: options?.name ?? 'rvf-mcp-server',
|
|
80
|
+
stores: server.storeCount,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
app.listen(port, () => {
|
|
85
|
+
console.error(`RVF MCP Server (SSE) listening on http://localhost:${port}`);
|
|
86
|
+
console.error(` SSE endpoint: http://localhost:${port}/sse`);
|
|
87
|
+
console.error(` Message endpoint: http://localhost:${port}/messages`);
|
|
88
|
+
console.error(` Health check: http://localhost:${port}/health`);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return server;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create a server with the specified transport type.
|
|
96
|
+
*/
|
|
97
|
+
export async function createServer(
|
|
98
|
+
transport: 'stdio' | 'sse' = 'stdio',
|
|
99
|
+
port = 3100,
|
|
100
|
+
options?: RvfMcpServerOptions,
|
|
101
|
+
): Promise<RvfMcpServer> {
|
|
102
|
+
if (transport === 'sse') {
|
|
103
|
+
return createSseServer(port, options);
|
|
104
|
+
}
|
|
105
|
+
return createStdioServer(options);
|
|
106
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"resolveJsonModule": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*"],
|
|
18
|
+
"exclude": ["node_modules", "dist"]
|
|
19
|
+
}
|