slimmcp-proxy 0.0.0-stage → 1.0.1
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 +74 -2
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +200 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config.d.ts +5 -0
- package/dist/src/config.js +63 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +11 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/indexer.d.ts +22 -0
- package/dist/src/indexer.js +131 -0
- package/dist/src/indexer.js.map +1 -0
- package/dist/src/ledger.d.ts +28 -0
- package/dist/src/ledger.js +106 -0
- package/dist/src/ledger.js.map +1 -0
- package/dist/src/license.d.ts +23 -0
- package/dist/src/license.js +126 -0
- package/dist/src/license.js.map +1 -0
- package/dist/src/policy.d.ts +77 -0
- package/dist/src/policy.js +103 -0
- package/dist/src/policy.js.map +1 -0
- package/dist/src/redactor.d.ts +2 -0
- package/dist/src/redactor.js +95 -0
- package/dist/src/redactor.js.map +1 -0
- package/dist/src/server.d.ts +43 -0
- package/dist/src/server.js +314 -0
- package/dist/src/server.js.map +1 -0
- package/dist/src/tokenizer.d.ts +11 -0
- package/dist/src/tokenizer.js +37 -0
- package/dist/src/tokenizer.js.map +1 -0
- package/dist/src/types.d.ts +88 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/upstream.d.ts +17 -0
- package/dist/src/upstream.js +62 -0
- package/dist/src/upstream.js.map +1 -0
- package/dist/tests/fixtures/mock-mcp-server.d.ts +1 -0
- package/dist/tests/fixtures/mock-mcp-server.js +81 -0
- package/dist/tests/fixtures/mock-mcp-server.js.map +1 -0
- package/dist/tests/slimmcp.test.d.ts +1 -0
- package/dist/tests/slimmcp.test.js +207 -0
- package/dist/tests/slimmcp.test.js.map +1 -0
- package/package.json +39 -4
- package/slimmcp.config.example.json +24 -0
- package/slimmcp.policy.example.json +19 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { redactSecrets } from './redactor.js';
|
|
6
|
+
import { countUtf8Bytes, estimateTokens } from './tokenizer.js';
|
|
7
|
+
export const META_TOOLS = [
|
|
8
|
+
{
|
|
9
|
+
name: 'discover_tools',
|
|
10
|
+
description: 'Search connected MCP tools by keyword or intent. Returns compact tool names, 1-line summaries, and required argument names without loading full JSON schemas.',
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
query: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'Search keywords or "*" to list tools.'
|
|
17
|
+
},
|
|
18
|
+
server: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'Optional upstream server ID filter.'
|
|
21
|
+
},
|
|
22
|
+
limit: {
|
|
23
|
+
type: 'number',
|
|
24
|
+
description: 'Max results to return (1-25, default 8).'
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
required: ['query']
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'inspect_tool_schema',
|
|
32
|
+
description: 'Fetch the exact JSON inputSchema for a single discovered MCP tool before invoking it.',
|
|
33
|
+
inputSchema: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
server: { type: 'string', description: 'Upstream server ID.' },
|
|
37
|
+
tool: { type: 'string', description: 'Exact tool name.' }
|
|
38
|
+
},
|
|
39
|
+
required: ['server', 'tool']
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'invoke_tool',
|
|
44
|
+
description: 'Execute a specific upstream MCP tool with secret redaction and Pro policy/spend-cap enforcement.',
|
|
45
|
+
inputSchema: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
server: { type: 'string', description: 'Upstream server ID.' },
|
|
49
|
+
tool: { type: 'string', description: 'Exact tool name.' },
|
|
50
|
+
arguments: {
|
|
51
|
+
type: 'object',
|
|
52
|
+
description: 'Arguments object matching inspect_tool_schema.'
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
required: ['server', 'tool']
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
];
|
|
59
|
+
const DiscoverInputSchema = z.object({
|
|
60
|
+
query: z.string().default('*'),
|
|
61
|
+
server: z.string().optional(),
|
|
62
|
+
limit: z.number().int().min(1).max(25).optional()
|
|
63
|
+
});
|
|
64
|
+
const InspectInputSchema = z.object({
|
|
65
|
+
server: z.string().min(1),
|
|
66
|
+
tool: z.string().min(1)
|
|
67
|
+
});
|
|
68
|
+
const InvokeInputSchema = z.object({
|
|
69
|
+
server: z.string().min(1),
|
|
70
|
+
tool: z.string().min(1),
|
|
71
|
+
arguments: z.record(z.unknown()).optional()
|
|
72
|
+
});
|
|
73
|
+
export class SlimProxyCore {
|
|
74
|
+
opts;
|
|
75
|
+
constructor(opts) {
|
|
76
|
+
this.opts = opts;
|
|
77
|
+
}
|
|
78
|
+
async handleListTools() {
|
|
79
|
+
return { tools: META_TOOLS };
|
|
80
|
+
}
|
|
81
|
+
async handleCallTool(name, rawArgs) {
|
|
82
|
+
const start = performance.now();
|
|
83
|
+
const { config, upstream, indexer, policyGuard, ledger, sessionId } = this.opts;
|
|
84
|
+
if (name === 'discover_tools') {
|
|
85
|
+
const parsed = DiscoverInputSchema.safeParse(rawArgs ?? {});
|
|
86
|
+
if (!parsed.success) {
|
|
87
|
+
return {
|
|
88
|
+
isError: true,
|
|
89
|
+
content: [{ type: 'text', text: `Invalid discover_tools args: ${parsed.error.message}` }]
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const matches = indexer.search(parsed.data.query, {
|
|
93
|
+
server: parsed.data.server,
|
|
94
|
+
limit: parsed.data.limit
|
|
95
|
+
});
|
|
96
|
+
const elapsed = performance.now() - start;
|
|
97
|
+
ledger.recordEvent({
|
|
98
|
+
sessionId,
|
|
99
|
+
kind: 'discover_tools',
|
|
100
|
+
tokensSaved: 0,
|
|
101
|
+
status: 'allowed',
|
|
102
|
+
latencyMs: elapsed
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
content: [
|
|
106
|
+
{
|
|
107
|
+
type: 'text',
|
|
108
|
+
text: JSON.stringify({ matches, totalIndexedTools: indexer.getAllTools().length })
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (name === 'inspect_tool_schema') {
|
|
114
|
+
const parsed = InspectInputSchema.safeParse(rawArgs ?? {});
|
|
115
|
+
if (!parsed.success) {
|
|
116
|
+
return {
|
|
117
|
+
isError: true,
|
|
118
|
+
content: [
|
|
119
|
+
{ type: 'text', text: `Invalid inspect_tool_schema args: ${parsed.error.message}` }
|
|
120
|
+
]
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const tool = indexer.getTool(parsed.data.server, parsed.data.tool);
|
|
124
|
+
const elapsed = performance.now() - start;
|
|
125
|
+
if (!tool) {
|
|
126
|
+
ledger.recordEvent({
|
|
127
|
+
sessionId,
|
|
128
|
+
kind: 'inspect_tool_schema',
|
|
129
|
+
serverId: parsed.data.server,
|
|
130
|
+
toolName: parsed.data.tool,
|
|
131
|
+
status: 'error',
|
|
132
|
+
latencyMs: elapsed
|
|
133
|
+
});
|
|
134
|
+
return {
|
|
135
|
+
isError: true,
|
|
136
|
+
content: [
|
|
137
|
+
{
|
|
138
|
+
type: 'text',
|
|
139
|
+
text: `Tool "${parsed.data.server}:${parsed.data.tool}" not found. Call discover_tools first.`
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
ledger.recordEvent({
|
|
145
|
+
sessionId,
|
|
146
|
+
kind: 'inspect_tool_schema',
|
|
147
|
+
serverId: tool.serverId,
|
|
148
|
+
toolName: tool.name,
|
|
149
|
+
status: 'allowed',
|
|
150
|
+
latencyMs: elapsed
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
content: [
|
|
154
|
+
{
|
|
155
|
+
type: 'text',
|
|
156
|
+
text: JSON.stringify({
|
|
157
|
+
server: tool.serverId,
|
|
158
|
+
tool: tool.name,
|
|
159
|
+
description: tool.description,
|
|
160
|
+
inputSchema: tool.inputSchema
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
if (name === 'invoke_tool') {
|
|
167
|
+
const parsed = InvokeInputSchema.safeParse(rawArgs ?? {});
|
|
168
|
+
if (!parsed.success) {
|
|
169
|
+
return {
|
|
170
|
+
isError: true,
|
|
171
|
+
content: [{ type: 'text', text: `Invalid invoke_tool args: ${parsed.error.message}` }]
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const { server, tool } = parsed.data;
|
|
175
|
+
const indexedTool = indexer.getTool(server, tool);
|
|
176
|
+
if (!indexedTool) {
|
|
177
|
+
return {
|
|
178
|
+
isError: true,
|
|
179
|
+
content: [
|
|
180
|
+
{
|
|
181
|
+
type: 'text',
|
|
182
|
+
text: `Tool "${server}:${tool}" not found. Call discover_tools first.`
|
|
183
|
+
}
|
|
184
|
+
]
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const decision = policyGuard.evaluate(server, tool);
|
|
188
|
+
if (!decision.allowed) {
|
|
189
|
+
const elapsed = performance.now() - start;
|
|
190
|
+
const status = decision.code === 'DENIED_BY_POLICY' || decision.code === 'PRO_LICENSE_REQUIRED'
|
|
191
|
+
? 'blocked_policy'
|
|
192
|
+
: 'blocked_cap';
|
|
193
|
+
ledger.recordEvent({
|
|
194
|
+
sessionId,
|
|
195
|
+
kind: 'invoke_tool',
|
|
196
|
+
serverId: server,
|
|
197
|
+
toolName: tool,
|
|
198
|
+
status,
|
|
199
|
+
latencyMs: elapsed
|
|
200
|
+
});
|
|
201
|
+
return {
|
|
202
|
+
isError: true,
|
|
203
|
+
content: [{ type: 'text', text: `[${decision.code}] ${decision.reason}` }]
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const redactionEnabled = config.redaction?.enabled ?? true;
|
|
207
|
+
const redactInputs = redactionEnabled && (config.redaction?.redactInputs ?? true);
|
|
208
|
+
const redactOutputs = redactionEnabled && (config.redaction?.redactOutputs ?? true);
|
|
209
|
+
let callArgs = parsed.data.arguments ?? {};
|
|
210
|
+
let redactionsCount = 0;
|
|
211
|
+
if (redactInputs) {
|
|
212
|
+
const res = redactSecrets(callArgs);
|
|
213
|
+
callArgs = res.sanitized;
|
|
214
|
+
redactionsCount += res.redactionCount;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
let upstreamResult = await upstream.callTool(server, tool, callArgs);
|
|
218
|
+
if (redactOutputs) {
|
|
219
|
+
const outRes = redactSecrets(upstreamResult);
|
|
220
|
+
upstreamResult = outRes.sanitized;
|
|
221
|
+
redactionsCount += outRes.redactionCount;
|
|
222
|
+
}
|
|
223
|
+
const elapsed = performance.now() - start;
|
|
224
|
+
ledger.recordEvent({
|
|
225
|
+
sessionId,
|
|
226
|
+
kind: 'invoke_tool',
|
|
227
|
+
serverId: server,
|
|
228
|
+
toolName: tool,
|
|
229
|
+
status: 'allowed',
|
|
230
|
+
latencyMs: elapsed,
|
|
231
|
+
redactionsCount
|
|
232
|
+
});
|
|
233
|
+
return upstreamResult;
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
const elapsed = performance.now() - start;
|
|
237
|
+
ledger.recordEvent({
|
|
238
|
+
sessionId,
|
|
239
|
+
kind: 'invoke_tool',
|
|
240
|
+
serverId: server,
|
|
241
|
+
toolName: tool,
|
|
242
|
+
status: 'error',
|
|
243
|
+
latencyMs: elapsed,
|
|
244
|
+
redactionsCount
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
isError: true,
|
|
248
|
+
content: [
|
|
249
|
+
{
|
|
250
|
+
type: 'text',
|
|
251
|
+
text: `Upstream error calling ${server}:${tool}: ${err instanceof Error ? err.message : String(err)}`
|
|
252
|
+
}
|
|
253
|
+
]
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
isError: true,
|
|
259
|
+
content: [{ type: 'text', text: `Unknown meta-tool "${name}".` }]
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
export function computeSchemaSavings(serverTools) {
|
|
264
|
+
const allRawTools = serverTools.flatMap((s) => s.tools);
|
|
265
|
+
const rawPayload = { tools: allRawTools };
|
|
266
|
+
const slimPayload = { tools: META_TOOLS };
|
|
267
|
+
const rawSchemaBytes = countUtf8Bytes(rawPayload);
|
|
268
|
+
const rawSchemaTokens = estimateTokens(rawPayload);
|
|
269
|
+
const slimExposedBytes = countUtf8Bytes(slimPayload);
|
|
270
|
+
const slimExposedTokens = estimateTokens(slimPayload);
|
|
271
|
+
const tokensSaved = Math.max(0, rawSchemaTokens - slimExposedTokens);
|
|
272
|
+
const reductionPercent = rawSchemaTokens > 0
|
|
273
|
+
? Number(((tokensSaved / rawSchemaTokens) * 100).toFixed(2))
|
|
274
|
+
: 0;
|
|
275
|
+
return {
|
|
276
|
+
rawToolsCount: allRawTools.length,
|
|
277
|
+
rawSchemaBytes,
|
|
278
|
+
rawSchemaTokens,
|
|
279
|
+
slimToolsCount: META_TOOLS.length,
|
|
280
|
+
slimExposedBytes,
|
|
281
|
+
slimExposedTokens,
|
|
282
|
+
tokensSaved,
|
|
283
|
+
reductionPercent
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
export async function startStdioProxyServer(core, upstream, ledger) {
|
|
287
|
+
const server = new Server({ name: 'slimmcp-proxy', version: '1.0.0' }, { capabilities: { tools: {} } });
|
|
288
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
289
|
+
return core.handleListTools();
|
|
290
|
+
});
|
|
291
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
292
|
+
return core.handleCallTool(request.params.name, request.params.arguments);
|
|
293
|
+
});
|
|
294
|
+
const transport = new StdioServerTransport();
|
|
295
|
+
let closed = false;
|
|
296
|
+
const shutdown = async () => {
|
|
297
|
+
if (closed)
|
|
298
|
+
return;
|
|
299
|
+
closed = true;
|
|
300
|
+
await upstream.closeAll();
|
|
301
|
+
ledger.close();
|
|
302
|
+
process.exit(0);
|
|
303
|
+
};
|
|
304
|
+
server.onclose = () => {
|
|
305
|
+
void shutdown();
|
|
306
|
+
};
|
|
307
|
+
process.stdin.on('end', () => {
|
|
308
|
+
void shutdown();
|
|
309
|
+
});
|
|
310
|
+
process.on('SIGINT', () => void shutdown());
|
|
311
|
+
process.on('SIGTERM', () => void shutdown());
|
|
312
|
+
await server.connect(transport);
|
|
313
|
+
}
|
|
314
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAIhE,MAAM,CAAC,MAAM,UAAU,GAAiB;IACtC;QACE,IAAI,EAAE,gBAAgB;QACtB,WAAW,EACT,+JAA+J;QACjK,WAAW,E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,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;IAEH,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,MAAM,QAAQ,GAAG,KAAK,IAAI,EAAE;QAC1B,IAAI,MAAM;YAAE,OAAO;QACnB,MAAM,GAAG,IAAI,CAAC;QACd,MAAM,QAAQ,CAAC,QAAQ,EAAE,CAAC;QAC1B,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,MAAM,CAAC,OAAO,GAAG,GAAG,EAAE;QACpB,KAAK,QAAQ,EAAE,CAAC;IAClB,CAAC,CAAC;IACF,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;QAC3B,KAAK,QAAQ,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,KAAK,QAAQ,EAAE,CAAC,CAAC;IAC5C,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,QAAQ,EAAE,CAAC,CAAC;IAE7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic UTF-8 byte & heuristic token estimator (~4 UTF-8 bytes per token).
|
|
3
|
+
* Always reports exact UTF-8 byte counts alongside heuristic token estimates.
|
|
4
|
+
*/
|
|
5
|
+
export declare function countUtf8Bytes(value: unknown): number;
|
|
6
|
+
export declare function estimateTokens(value: unknown): number;
|
|
7
|
+
export declare function compactToolSummary(description: string | undefined, inputSchema: Record<string, unknown> | undefined): {
|
|
8
|
+
summary: string;
|
|
9
|
+
requiredArgs: string[];
|
|
10
|
+
optionalArgCount: number;
|
|
11
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic UTF-8 byte & heuristic token estimator (~4 UTF-8 bytes per token).
|
|
3
|
+
* Always reports exact UTF-8 byte counts alongside heuristic token estimates.
|
|
4
|
+
*/
|
|
5
|
+
export function countUtf8Bytes(value) {
|
|
6
|
+
const serialized = typeof value === 'string' ? value : JSON.stringify(value ?? '');
|
|
7
|
+
return Buffer.byteLength(serialized, 'utf8');
|
|
8
|
+
}
|
|
9
|
+
export function estimateTokens(value) {
|
|
10
|
+
const bytes = countUtf8Bytes(value);
|
|
11
|
+
if (bytes === 0)
|
|
12
|
+
return 0;
|
|
13
|
+
return Math.ceil(bytes / 4);
|
|
14
|
+
}
|
|
15
|
+
export function compactToolSummary(description, inputSchema) {
|
|
16
|
+
const cleanDesc = (description ?? '')
|
|
17
|
+
.replace(/\s+/g, ' ')
|
|
18
|
+
.trim();
|
|
19
|
+
const firstSentence = cleanDesc.split(/(?<=[.!?])\s+/)[0] ?? cleanDesc;
|
|
20
|
+
const summary = firstSentence.length > 140
|
|
21
|
+
? `${firstSentence.slice(0, 137)}...`
|
|
22
|
+
: firstSentence || 'No description provided.';
|
|
23
|
+
const props = inputSchema && typeof inputSchema.properties === 'object' && inputSchema.properties !== null
|
|
24
|
+
? Object.keys(inputSchema.properties)
|
|
25
|
+
: [];
|
|
26
|
+
const requiredSet = new Set(Array.isArray(inputSchema?.required)
|
|
27
|
+
? inputSchema.required.filter((r) => typeof r === 'string')
|
|
28
|
+
: []);
|
|
29
|
+
const requiredArgs = props.filter((p) => requiredSet.has(p));
|
|
30
|
+
for (const r of requiredSet) {
|
|
31
|
+
if (!requiredArgs.includes(r))
|
|
32
|
+
requiredArgs.push(r);
|
|
33
|
+
}
|
|
34
|
+
const optionalArgCount = Math.max(0, props.length - requiredArgs.length);
|
|
35
|
+
return { summary, requiredArgs, optionalArgCount };
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=tokenizer.js.map
|
|
@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CAAC,EAAE,CAAC;IACT,MAAM,WAAW,GAAG,IAAI,GAAG,CACzB,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC;QAClC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC;QACxE,CAAC,CAAC,EAAE,CACP,CAAC;IAEF,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7D,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC;QAC5B,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;YAAE,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACtD,CAAC;IACD,MAAM,gBAAgB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IAEzE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export interface UpstreamServerConfig {
|
|
2
|
+
id: string;
|
|
3
|
+
command: string;
|
|
4
|
+
args?: string[];
|
|
5
|
+
env?: Record<string, string>;
|
|
6
|
+
cwd?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface SlimMcpConfig {
|
|
9
|
+
servers: UpstreamServerConfig[];
|
|
10
|
+
redaction?: {
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
redactInputs?: boolean;
|
|
13
|
+
redactOutputs?: boolean;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface RawMcpTool {
|
|
17
|
+
name: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
inputSchema?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
export interface IndexedTool {
|
|
22
|
+
key: string;
|
|
23
|
+
serverId: string;
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
summary: string;
|
|
27
|
+
requiredArgs: string[];
|
|
28
|
+
optionalArgCount: number;
|
|
29
|
+
inputSchema: Record<string, unknown>;
|
|
30
|
+
rawBytes: number;
|
|
31
|
+
rawTokens: number;
|
|
32
|
+
}
|
|
33
|
+
export interface ToolMatch {
|
|
34
|
+
server: string;
|
|
35
|
+
tool: string;
|
|
36
|
+
summary: string;
|
|
37
|
+
requiredArgs: string[];
|
|
38
|
+
optionalArgCount: number;
|
|
39
|
+
score: number;
|
|
40
|
+
}
|
|
41
|
+
export interface PolicyRule {
|
|
42
|
+
pattern: string;
|
|
43
|
+
action: 'allow' | 'deny';
|
|
44
|
+
maxCallsPerSession?: number;
|
|
45
|
+
estimatedX402AtomicUsdc?: number;
|
|
46
|
+
}
|
|
47
|
+
export interface PolicyConfig {
|
|
48
|
+
defaultAction: 'allow' | 'deny';
|
|
49
|
+
sessionLimits?: {
|
|
50
|
+
maxCallsPerSession?: number;
|
|
51
|
+
maxX402AtomicUsdcPerSession?: number;
|
|
52
|
+
};
|
|
53
|
+
rules?: PolicyRule[];
|
|
54
|
+
}
|
|
55
|
+
export interface ProLicensePayload {
|
|
56
|
+
v: 1;
|
|
57
|
+
product: 'slimmcp-proxy';
|
|
58
|
+
tier: 'indie' | 'team';
|
|
59
|
+
payer: string;
|
|
60
|
+
txHash: string;
|
|
61
|
+
issuedAt: number;
|
|
62
|
+
expiresAt: number | null;
|
|
63
|
+
}
|
|
64
|
+
export interface LicenseVerificationResult {
|
|
65
|
+
valid: boolean;
|
|
66
|
+
reason?: string;
|
|
67
|
+
payload?: ProLicensePayload;
|
|
68
|
+
}
|
|
69
|
+
export interface RedactionResult<T = unknown> {
|
|
70
|
+
sanitized: T;
|
|
71
|
+
redactionCount: number;
|
|
72
|
+
matchedRules: string[];
|
|
73
|
+
}
|
|
74
|
+
export interface StatsSummary {
|
|
75
|
+
sessionsCount: number;
|
|
76
|
+
totalEvents: number;
|
|
77
|
+
allowedCalls: number;
|
|
78
|
+
blockedPolicyCalls: number;
|
|
79
|
+
blockedCapCalls: number;
|
|
80
|
+
totalRedactions: number;
|
|
81
|
+
latestRawSchemaBytes: number;
|
|
82
|
+
latestRawSchemaTokens: number;
|
|
83
|
+
latestSlimExposedBytes: number;
|
|
84
|
+
latestSlimExposedTokens: number;
|
|
85
|
+
latestReductionPercent: number;
|
|
86
|
+
cumulativeTokensSaved: number;
|
|
87
|
+
avgLatencyMs: number;
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RawMcpTool, UpstreamServerConfig } from './types.js';
|
|
2
|
+
export declare class UpstreamManager {
|
|
3
|
+
private connections;
|
|
4
|
+
connectAll(configs: UpstreamServerConfig[]): Promise<Array<{
|
|
5
|
+
serverId: string;
|
|
6
|
+
tools: RawMcpTool[];
|
|
7
|
+
}>>;
|
|
8
|
+
callTool(serverId: string, toolName: string, args: Record<string, unknown>): Promise<{
|
|
9
|
+
content: Array<{
|
|
10
|
+
type: string;
|
|
11
|
+
text?: string;
|
|
12
|
+
[k: string]: unknown;
|
|
13
|
+
}>;
|
|
14
|
+
isError?: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
closeAll(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
3
|
+
export class UpstreamManager {
|
|
4
|
+
connections = new Map();
|
|
5
|
+
async connectAll(configs) {
|
|
6
|
+
const results = [];
|
|
7
|
+
for (const cfg of configs) {
|
|
8
|
+
const mergedEnv = cfg.env
|
|
9
|
+
? Object.fromEntries(Object.entries({ ...process.env, ...cfg.env }).filter((pair) => typeof pair[1] === 'string'))
|
|
10
|
+
: undefined;
|
|
11
|
+
const transport = new StdioClientTransport({
|
|
12
|
+
command: cfg.command,
|
|
13
|
+
args: cfg.args ?? [],
|
|
14
|
+
env: mergedEnv,
|
|
15
|
+
cwd: cfg.cwd,
|
|
16
|
+
stderr: 'pipe'
|
|
17
|
+
});
|
|
18
|
+
const client = new Client({ name: 'slimmcp-proxy-client', version: '1.0.0' }, { capabilities: {} });
|
|
19
|
+
await client.connect(transport);
|
|
20
|
+
const listResult = await client.listTools();
|
|
21
|
+
const tools = (listResult.tools ?? []).map((t) => ({
|
|
22
|
+
name: t.name,
|
|
23
|
+
description: t.description,
|
|
24
|
+
inputSchema: t.inputSchema ?? {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {}
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
this.connections.set(cfg.id, {
|
|
30
|
+
config: cfg,
|
|
31
|
+
client,
|
|
32
|
+
transport,
|
|
33
|
+
tools
|
|
34
|
+
});
|
|
35
|
+
results.push({ serverId: cfg.id, tools });
|
|
36
|
+
}
|
|
37
|
+
return results;
|
|
38
|
+
}
|
|
39
|
+
async callTool(serverId, toolName, args) {
|
|
40
|
+
const conn = this.connections.get(serverId);
|
|
41
|
+
if (!conn) {
|
|
42
|
+
throw new Error(`Unknown upstream server "${serverId}".`);
|
|
43
|
+
}
|
|
44
|
+
const result = await conn.client.callTool({
|
|
45
|
+
name: toolName,
|
|
46
|
+
arguments: args
|
|
47
|
+
});
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
async closeAll() {
|
|
51
|
+
for (const conn of this.connections.values()) {
|
|
52
|
+
try {
|
|
53
|
+
await conn.client.close();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Ignore teardown errors during shutdown
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
this.connections.clear();
|
|
60
|
+
}
|
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CAAC;YAC5B,CAAC;YAAC,MAAM,CAAC;gBACP,yCAAyC;YAC3C,CAAC;QACH,CAAC;QACD,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
function getArg(prefix, fallback) {
|
|
5
|
+
const found = process.argv.find((a) => a.startsWith(prefix));
|
|
6
|
+
return found ? found.slice(prefix.length) : fallback;
|
|
7
|
+
}
|
|
8
|
+
const serverName = getArg('--server=', 'demo');
|
|
9
|
+
const count = Math.max(1, Number(getArg('--count=', '20')));
|
|
10
|
+
const verbs = [
|
|
11
|
+
'list',
|
|
12
|
+
'search',
|
|
13
|
+
'get',
|
|
14
|
+
'create',
|
|
15
|
+
'update',
|
|
16
|
+
'delete',
|
|
17
|
+
'export',
|
|
18
|
+
'audit',
|
|
19
|
+
'sync',
|
|
20
|
+
'pay_x402'
|
|
21
|
+
];
|
|
22
|
+
const tools = Array.from({ length: count }, (_, idx) => {
|
|
23
|
+
const verb = verbs[idx % verbs.length];
|
|
24
|
+
const toolName = `${verb}_${serverName}_resource_${idx + 1}`;
|
|
25
|
+
return {
|
|
26
|
+
name: toolName,
|
|
27
|
+
description: `Executes ${verb} operation #${idx + 1} on the ${serverName} service. Supports paginated filtering, structured metadata payloads, dry-run validation, and detailed diagnostic responses for automated engineering workflows.`,
|
|
28
|
+
inputSchema: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
targetId: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
description: `Unique identifier for the ${serverName} resource item to inspect or mutate.`
|
|
34
|
+
},
|
|
35
|
+
query: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
description: 'Optional filter expression or search query string.'
|
|
38
|
+
},
|
|
39
|
+
limit: {
|
|
40
|
+
type: 'number',
|
|
41
|
+
description: 'Maximum number of records to return per page (1-100).'
|
|
42
|
+
},
|
|
43
|
+
dryRun: {
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
description: 'When true, validates parameters without mutating remote state.'
|
|
46
|
+
},
|
|
47
|
+
payload: {
|
|
48
|
+
type: 'object',
|
|
49
|
+
description: 'Arbitrary structured metadata or JSON body for the request.',
|
|
50
|
+
properties: {
|
|
51
|
+
note: { type: 'string', description: 'Human-readable audit note.' },
|
|
52
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Tag labels.' }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
required: ['targetId']
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
const server = new Server({ name: `mock-${serverName}`, version: '1.0.0' }, { capabilities: { tools: {} } });
|
|
61
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
62
|
+
return { tools };
|
|
63
|
+
});
|
|
64
|
+
server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
65
|
+
const args = (req.params.arguments ?? {});
|
|
66
|
+
// Echo args cleanly (plus optional secret fixture test if simulateOutputSecret=true)
|
|
67
|
+
const extraSecret = args.simulateOutputSecret === true
|
|
68
|
+
? ' leaked_output=ghp_1234567890abcdefghij1234567890abcd'
|
|
69
|
+
: '';
|
|
70
|
+
return {
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
type: 'text',
|
|
74
|
+
text: `executed:${serverName}:${req.params.name}:${JSON.stringify(args)}${extraSecret}`
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const transport = new StdioServerTransport();
|
|
80
|
+
await server.connect(transport);
|
|
81
|
+
//# sourceMappingURL=mock-mcp-server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