@lightsparkdev/grid-mcp 0.3.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/LICENSE +201 -0
- package/README.md +107 -0
- package/code-tool-types.d.mts +14 -0
- package/code-tool-types.d.mts.map +1 -0
- package/code-tool-types.d.ts +14 -0
- package/code-tool-types.d.ts.map +1 -0
- package/code-tool-types.js +4 -0
- package/code-tool-types.js.map +1 -0
- package/code-tool-types.mjs +3 -0
- package/code-tool-types.mjs.map +1 -0
- package/code-tool.d.mts +15 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +15 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +116 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +113 -0
- package/code-tool.mjs.map +1 -0
- package/docs-search-tool.d.mts +51 -0
- package/docs-search-tool.d.mts.map +1 -0
- package/docs-search-tool.d.ts +51 -0
- package/docs-search-tool.d.ts.map +1 -0
- package/docs-search-tool.js +51 -0
- package/docs-search-tool.js.map +1 -0
- package/docs-search-tool.mjs +47 -0
- package/docs-search-tool.mjs.map +1 -0
- package/headers.d.mts +4 -0
- package/headers.d.mts.map +1 -0
- package/headers.d.ts +4 -0
- package/headers.d.ts.map +1 -0
- package/headers.js +35 -0
- package/headers.js.map +1 -0
- package/headers.mjs +31 -0
- package/headers.mjs.map +1 -0
- package/http.d.mts +14 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +14 -0
- package/http.d.ts.map +1 -0
- package/http.js +105 -0
- package/http.js.map +1 -0
- package/http.mjs +97 -0
- package/http.mjs.map +1 -0
- package/index.d.mts +3 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -0
- package/index.js +59 -0
- package/index.js.map +1 -0
- package/index.mjs +57 -0
- package/index.mjs.map +1 -0
- package/methods.d.mts +10 -0
- package/methods.d.mts.map +1 -0
- package/methods.d.ts +10 -0
- package/methods.d.ts.map +1 -0
- package/methods.js +328 -0
- package/methods.js.map +1 -0
- package/methods.mjs +324 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +15 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +15 -0
- package/options.d.ts.map +1 -0
- package/options.js +91 -0
- package/options.js.map +1 -0
- package/options.mjs +84 -0
- package/options.mjs.map +1 -0
- package/package.json +174 -0
- package/server.d.mts +30 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +30 -0
- package/server.d.ts.map +1 -0
- package/server.js +160 -0
- package/server.js.map +1 -0
- package/server.mjs +147 -0
- package/server.mjs.map +1 -0
- package/src/code-tool-types.ts +16 -0
- package/src/code-tool.ts +140 -0
- package/src/docs-search-tool.ts +59 -0
- package/src/headers.ts +39 -0
- package/src/http.ts +130 -0
- package/src/index.ts +65 -0
- package/src/methods.ts +348 -0
- package/src/options.ts +118 -0
- package/src/server.ts +195 -0
- package/src/stdio.ts +13 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +115 -0
- package/stdio.d.mts +3 -0
- package/stdio.d.mts.map +1 -0
- package/stdio.d.ts +3 -0
- package/stdio.d.ts.map +1 -0
- package/stdio.js +14 -0
- package/stdio.js.map +1 -0
- package/stdio.mjs +10 -0
- package/stdio.mjs.map +1 -0
- package/types.d.mts +52 -0
- package/types.d.mts.map +1 -0
- package/types.d.ts +52 -0
- package/types.d.ts.map +1 -0
- package/types.js +58 -0
- package/types.js.map +1 -0
- package/types.mjs +53 -0
- package/types.mjs.map +1 -0
package/src/code-tool.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { McpTool, Metadata, ToolCallResult, asErrorResult, asTextContentResult } from './types';
|
|
4
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
import { readEnv, requireValue } from './server';
|
|
6
|
+
import { WorkerInput, WorkerOutput } from './code-tool-types';
|
|
7
|
+
import { SdkMethod } from './methods';
|
|
8
|
+
import { LightsparkGrid } from '@lightsparkdev/grid';
|
|
9
|
+
|
|
10
|
+
const prompt = `Runs JavaScript code to interact with the Lightspark Grid API.
|
|
11
|
+
|
|
12
|
+
You are a skilled programmer writing code to interface with the service.
|
|
13
|
+
Define an async function named "run" that takes a single parameter of an initialized SDK client and it will be run.
|
|
14
|
+
For example:
|
|
15
|
+
|
|
16
|
+
\`\`\`
|
|
17
|
+
async function run(client) {
|
|
18
|
+
const quote = await client.quotes.create({
|
|
19
|
+
destination: { accountId: 'ExternalAccount:a12dcbd6-dced-4ec4-b756-3c3a9ea3d123', destinationType: 'ACCOUNT' },
|
|
20
|
+
lockedCurrencyAmount: 10000,
|
|
21
|
+
lockedCurrencySide: 'SENDING',
|
|
22
|
+
source: { accountId: 'InternalAccount:e85dcbd6-dced-4ec4-b756-3c3a9ea3d965', sourceType: 'ACCOUNT' },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
console.log(quote.createdAt);
|
|
26
|
+
}
|
|
27
|
+
\`\`\`
|
|
28
|
+
|
|
29
|
+
You will be returned anything that your function returns, plus the results of any console.log statements.
|
|
30
|
+
Do not add try-catch blocks for single API calls. The tool will handle errors for you.
|
|
31
|
+
Do not add comments unless necessary for generating better code.
|
|
32
|
+
Code will run in a container, and cannot interact with the network outside of the given SDK client.
|
|
33
|
+
Variables will not persist between calls, so make sure to return or log any data you might need later.`;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A tool that runs code against a copy of the SDK.
|
|
37
|
+
*
|
|
38
|
+
* Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
|
|
39
|
+
* we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
|
|
40
|
+
* a generic endpoint that can be used to invoke any endpoint with the provided arguments.
|
|
41
|
+
*
|
|
42
|
+
* @param endpoints - The endpoints to include in the list.
|
|
43
|
+
*/
|
|
44
|
+
export function codeTool(params: { blockedMethods: SdkMethod[] | undefined }): McpTool {
|
|
45
|
+
const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
|
|
46
|
+
const tool: Tool = {
|
|
47
|
+
name: 'execute',
|
|
48
|
+
description: prompt,
|
|
49
|
+
inputSchema: {
|
|
50
|
+
type: 'object',
|
|
51
|
+
properties: {
|
|
52
|
+
code: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
description: 'Code to execute.',
|
|
55
|
+
},
|
|
56
|
+
intent: {
|
|
57
|
+
type: 'string',
|
|
58
|
+
description: 'Task you are trying to perform. Used for improving the service.',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
required: ['code'],
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
const handler = async (client: LightsparkGrid, args: any): Promise<ToolCallResult> => {
|
|
65
|
+
const code = args.code as string;
|
|
66
|
+
const intent = args.intent as string | undefined;
|
|
67
|
+
|
|
68
|
+
// Do very basic blocking of code that includes forbidden method names.
|
|
69
|
+
//
|
|
70
|
+
// WARNING: This is not secure against obfuscation and other evasion methods. If
|
|
71
|
+
// stronger security blocks are required, then these should be enforced in the downstream
|
|
72
|
+
// API (e.g., by having users call the MCP server with API keys with limited permissions).
|
|
73
|
+
if (params.blockedMethods) {
|
|
74
|
+
const blockedMatches = params.blockedMethods.filter((method) =>
|
|
75
|
+
code.includes(method.fullyQualifiedName),
|
|
76
|
+
);
|
|
77
|
+
if (blockedMatches.length > 0) {
|
|
78
|
+
return asErrorResult(
|
|
79
|
+
`The following methods have been blocked by the MCP server and cannot be used in code execution: ${blockedMatches
|
|
80
|
+
.map((m) => m.fullyQualifiedName)
|
|
81
|
+
.join(', ')}`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// this is not required, but passing a Stainless API key for the matching project_name
|
|
87
|
+
// will allow you to run code-mode queries against non-published versions of your SDK.
|
|
88
|
+
const stainlessAPIKey = readEnv('STAINLESS_API_KEY');
|
|
89
|
+
const codeModeEndpoint =
|
|
90
|
+
readEnv('CODE_MODE_ENDPOINT_URL') ?? 'https://api.stainless.com/api/ai/code-tool';
|
|
91
|
+
|
|
92
|
+
const res = await fetch(codeModeEndpoint, {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
headers: {
|
|
95
|
+
...(stainlessAPIKey && { Authorization: stainlessAPIKey }),
|
|
96
|
+
'Content-Type': 'application/json',
|
|
97
|
+
client_envs: JSON.stringify({
|
|
98
|
+
GRID_CLIENT_ID: requireValue(
|
|
99
|
+
readEnv('GRID_CLIENT_ID') ?? client.username,
|
|
100
|
+
'set GRID_CLIENT_ID environment variable or provide username client option',
|
|
101
|
+
),
|
|
102
|
+
GRID_CLIENT_SECRET: requireValue(
|
|
103
|
+
readEnv('GRID_CLIENT_SECRET') ?? client.password,
|
|
104
|
+
'set GRID_CLIENT_SECRET environment variable or provide password client option',
|
|
105
|
+
),
|
|
106
|
+
GRID_WEBHOOK_PUBKEY: readEnv('GRID_WEBHOOK_PUBKEY') ?? client.webhookSignature ?? undefined,
|
|
107
|
+
LIGHTSPARK_GRID_BASE_URL: readEnv('LIGHTSPARK_GRID_BASE_URL') ?? client.baseURL ?? undefined,
|
|
108
|
+
}),
|
|
109
|
+
},
|
|
110
|
+
body: JSON.stringify({
|
|
111
|
+
project_name: 'grid',
|
|
112
|
+
code,
|
|
113
|
+
intent,
|
|
114
|
+
client_opts: {},
|
|
115
|
+
} satisfies WorkerInput),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!res.ok) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`${res.status}: ${
|
|
121
|
+
res.statusText
|
|
122
|
+
} error when trying to contact Code Tool server. Details: ${await res.text()}`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const { is_error, result, log_lines, err_lines } = (await res.json()) as WorkerOutput;
|
|
127
|
+
const hasLogs = log_lines.length > 0 || err_lines.length > 0;
|
|
128
|
+
const output = {
|
|
129
|
+
result,
|
|
130
|
+
...(log_lines.length > 0 && { log_lines }),
|
|
131
|
+
...(err_lines.length > 0 && { err_lines }),
|
|
132
|
+
};
|
|
133
|
+
if (is_error) {
|
|
134
|
+
return asErrorResult(typeof result === 'string' && !hasLogs ? result : JSON.stringify(output, null, 2));
|
|
135
|
+
}
|
|
136
|
+
return asTextContentResult(output);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return { metadata, tool, handler };
|
|
140
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Metadata, asTextContentResult } from './types';
|
|
4
|
+
|
|
5
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
6
|
+
|
|
7
|
+
export const metadata: Metadata = {
|
|
8
|
+
resource: 'all',
|
|
9
|
+
operation: 'read',
|
|
10
|
+
tags: [],
|
|
11
|
+
httpMethod: 'get',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const tool: Tool = {
|
|
15
|
+
name: 'search_docs',
|
|
16
|
+
description: 'Search for documentation for how to use the client to interact with the API.',
|
|
17
|
+
inputSchema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
query: {
|
|
21
|
+
type: 'string',
|
|
22
|
+
description: 'The query to search for.',
|
|
23
|
+
},
|
|
24
|
+
language: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
description: 'The language for the SDK to search for.',
|
|
27
|
+
enum: ['http', 'python', 'go', 'typescript', 'javascript', 'terraform', 'ruby', 'java', 'kotlin'],
|
|
28
|
+
},
|
|
29
|
+
detail: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'The amount of detail to return.',
|
|
32
|
+
enum: ['default', 'verbose'],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
required: ['query', 'language'],
|
|
36
|
+
},
|
|
37
|
+
annotations: {
|
|
38
|
+
readOnlyHint: true,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const docsSearchURL =
|
|
43
|
+
process.env['DOCS_SEARCH_URL'] || 'https://api.stainless.com/api/projects/grid/docs/search';
|
|
44
|
+
|
|
45
|
+
export const handler = async (_: unknown, args: Record<string, unknown> | undefined) => {
|
|
46
|
+
const body = args as any;
|
|
47
|
+
const query = new URLSearchParams(body).toString();
|
|
48
|
+
const result = await fetch(`${docsSearchURL}?${query}`);
|
|
49
|
+
|
|
50
|
+
if (!result.ok) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`${result.status}: ${result.statusText} when using doc search tool. Details: ${await result.text()}`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return asTextContentResult(await result.json());
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default { metadata, tool, handler };
|
package/src/headers.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { IncomingMessage } from 'node:http';
|
|
4
|
+
import { ClientOptions } from '@lightsparkdev/grid';
|
|
5
|
+
|
|
6
|
+
export const parseAuthHeaders = (req: IncomingMessage, required?: boolean): Partial<ClientOptions> => {
|
|
7
|
+
if (req.headers.authorization) {
|
|
8
|
+
const scheme = req.headers.authorization.split(' ')[0]!;
|
|
9
|
+
const value = req.headers.authorization.slice(scheme.length + 1);
|
|
10
|
+
switch (scheme) {
|
|
11
|
+
case 'Basic':
|
|
12
|
+
const rawValue = Buffer.from(value, 'base64').toString();
|
|
13
|
+
return {
|
|
14
|
+
username: rawValue.slice(0, rawValue.search(':')),
|
|
15
|
+
password: rawValue.slice(rawValue.search(':') + 1),
|
|
16
|
+
};
|
|
17
|
+
default:
|
|
18
|
+
throw new Error(
|
|
19
|
+
'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Basic).',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
} else if (required) {
|
|
23
|
+
throw new Error('Missing required Authorization header; see WWW-Authenticate header for details.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const username =
|
|
27
|
+
Array.isArray(req.headers['x-grid-client-id']) ?
|
|
28
|
+
req.headers['x-grid-client-id'][0]
|
|
29
|
+
: req.headers['x-grid-client-id'];
|
|
30
|
+
const password =
|
|
31
|
+
Array.isArray(req.headers['x-grid-client-secret']) ?
|
|
32
|
+
req.headers['x-grid-client-secret'][0]
|
|
33
|
+
: req.headers['x-grid-client-secret'];
|
|
34
|
+
const webhookSignature =
|
|
35
|
+
Array.isArray(req.headers['x-grid-signature']) ?
|
|
36
|
+
req.headers['x-grid-signature'][0]
|
|
37
|
+
: req.headers['x-grid-signature'];
|
|
38
|
+
return { username, password, webhookSignature };
|
|
39
|
+
};
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
4
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
5
|
+
import express from 'express';
|
|
6
|
+
import morgan from 'morgan';
|
|
7
|
+
import morganBody from 'morgan-body';
|
|
8
|
+
import { McpOptions } from './options';
|
|
9
|
+
import { ClientOptions, initMcpServer, newMcpServer } from './server';
|
|
10
|
+
import { parseAuthHeaders } from './headers';
|
|
11
|
+
|
|
12
|
+
const newServer = async ({
|
|
13
|
+
clientOptions,
|
|
14
|
+
mcpOptions,
|
|
15
|
+
req,
|
|
16
|
+
res,
|
|
17
|
+
}: {
|
|
18
|
+
clientOptions: ClientOptions;
|
|
19
|
+
mcpOptions: McpOptions;
|
|
20
|
+
req: express.Request;
|
|
21
|
+
res: express.Response;
|
|
22
|
+
}): Promise<McpServer | null> => {
|
|
23
|
+
const server = await newMcpServer();
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const authOptions = parseAuthHeaders(req, false);
|
|
27
|
+
await initMcpServer({
|
|
28
|
+
server: server,
|
|
29
|
+
mcpOptions: mcpOptions,
|
|
30
|
+
clientOptions: {
|
|
31
|
+
...clientOptions,
|
|
32
|
+
...authOptions,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
} catch (error) {
|
|
36
|
+
res.status(401).json({
|
|
37
|
+
jsonrpc: '2.0',
|
|
38
|
+
error: {
|
|
39
|
+
code: -32000,
|
|
40
|
+
message: `Unauthorized: ${error instanceof Error ? error.message : error}`,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return server;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const post =
|
|
50
|
+
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
|
|
51
|
+
async (req: express.Request, res: express.Response) => {
|
|
52
|
+
const server = await newServer({ ...options, req, res });
|
|
53
|
+
// If we return null, we already set the authorization error.
|
|
54
|
+
if (server === null) return;
|
|
55
|
+
const transport = new StreamableHTTPServerTransport();
|
|
56
|
+
await server.connect(transport as any);
|
|
57
|
+
await transport.handleRequest(req, res, req.body);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const get = async (req: express.Request, res: express.Response) => {
|
|
61
|
+
res.status(405).json({
|
|
62
|
+
jsonrpc: '2.0',
|
|
63
|
+
error: {
|
|
64
|
+
code: -32000,
|
|
65
|
+
message: 'Method not supported',
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const del = async (req: express.Request, res: express.Response) => {
|
|
71
|
+
res.status(405).json({
|
|
72
|
+
jsonrpc: '2.0',
|
|
73
|
+
error: {
|
|
74
|
+
code: -32000,
|
|
75
|
+
message: 'Method not supported',
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const streamableHTTPApp = ({
|
|
81
|
+
clientOptions = {},
|
|
82
|
+
mcpOptions,
|
|
83
|
+
debug,
|
|
84
|
+
}: {
|
|
85
|
+
clientOptions?: ClientOptions;
|
|
86
|
+
mcpOptions: McpOptions;
|
|
87
|
+
debug: boolean;
|
|
88
|
+
}): express.Express => {
|
|
89
|
+
const app = express();
|
|
90
|
+
app.set('query parser', 'extended');
|
|
91
|
+
app.use(express.json());
|
|
92
|
+
|
|
93
|
+
if (debug) {
|
|
94
|
+
morganBody(app, {
|
|
95
|
+
logAllReqHeader: true,
|
|
96
|
+
logAllResHeader: true,
|
|
97
|
+
logRequestBody: true,
|
|
98
|
+
logResponseBody: true,
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
app.use(morgan('combined'));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
app.get('/health', async (req: express.Request, res: express.Response) => {
|
|
105
|
+
res.status(200).send('OK');
|
|
106
|
+
});
|
|
107
|
+
app.get('/', get);
|
|
108
|
+
app.post('/', post({ clientOptions, mcpOptions }));
|
|
109
|
+
app.delete('/', del);
|
|
110
|
+
|
|
111
|
+
return app;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const launchStreamableHTTPServer = async (params: {
|
|
115
|
+
mcpOptions: McpOptions;
|
|
116
|
+
debug: boolean;
|
|
117
|
+
port: number | string | undefined;
|
|
118
|
+
}) => {
|
|
119
|
+
const app = streamableHTTPApp({ mcpOptions: params.mcpOptions, debug: params.debug });
|
|
120
|
+
const server = app.listen(params.port);
|
|
121
|
+
const address = server.address();
|
|
122
|
+
|
|
123
|
+
if (typeof address === 'string') {
|
|
124
|
+
console.error(`MCP Server running on streamable HTTP at ${address}`);
|
|
125
|
+
} else if (address !== null) {
|
|
126
|
+
console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
|
|
127
|
+
} else {
|
|
128
|
+
console.error(`MCP Server running on streamable HTTP on port ${params.port}`);
|
|
129
|
+
}
|
|
130
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { selectTools } from './server';
|
|
4
|
+
import { McpOptions, parseCLIOptions } from './options';
|
|
5
|
+
import { launchStdioServer } from './stdio';
|
|
6
|
+
import { launchStreamableHTTPServer } from './http';
|
|
7
|
+
import type { McpTool } from './types';
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
const options = parseOptionsOrError();
|
|
11
|
+
|
|
12
|
+
const selectedTools = await selectToolsOrError(options);
|
|
13
|
+
|
|
14
|
+
console.error(
|
|
15
|
+
`MCP Server starting with ${selectedTools.length} tools:`,
|
|
16
|
+
selectedTools.map((e) => e.tool.name),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
switch (options.transport) {
|
|
20
|
+
case 'stdio':
|
|
21
|
+
await launchStdioServer(options);
|
|
22
|
+
break;
|
|
23
|
+
case 'http':
|
|
24
|
+
await launchStreamableHTTPServer({
|
|
25
|
+
mcpOptions: options,
|
|
26
|
+
debug: options.debug,
|
|
27
|
+
port: options.port ?? options.socket,
|
|
28
|
+
});
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (require.main === module) {
|
|
34
|
+
main().catch((error) => {
|
|
35
|
+
console.error('Fatal error in main():', error);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseOptionsOrError() {
|
|
41
|
+
try {
|
|
42
|
+
return parseCLIOptions();
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('Error parsing options:', error);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
|
|
50
|
+
try {
|
|
51
|
+
const includedTools = selectTools(options);
|
|
52
|
+
if (includedTools.length === 0) {
|
|
53
|
+
console.error('No tools match the provided filters.');
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
return includedTools;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof Error) {
|
|
59
|
+
console.error('Error filtering tools:', error.message);
|
|
60
|
+
} else {
|
|
61
|
+
console.error('Error filtering tools:', error);
|
|
62
|
+
}
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
}
|