@cjavdev/believe-mcp 0.5.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/LICENSE +7 -0
- package/README.md +101 -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 +12 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +12 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +97 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +94 -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 +25 -0
- package/headers.js.map +1 -0
- package/headers.mjs +21 -0
- package/headers.mjs.map +1 -0
- package/http.d.mts +9 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +9 -0
- package/http.d.ts.map +1 -0
- package/http.js +90 -0
- package/http.js.map +1 -0
- package/http.mjs +82 -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 +55 -0
- package/index.js.map +1 -0
- package/index.mjs +53 -0
- package/index.mjs.map +1 -0
- package/options.d.mts +11 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +11 -0
- package/options.d.ts.map +1 -0
- package/options.js +73 -0
- package/options.js.map +1 -0
- package/options.mjs +66 -0
- package/options.mjs.map +1 -0
- package/package.json +163 -0
- package/server.d.mts +31 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +31 -0
- package/server.d.ts.map +1 -0
- package/server.js +128 -0
- package/server.js.map +1 -0
- package/server.mjs +115 -0
- package/server.mjs.map +1 -0
- package/src/code-tool-types.ts +16 -0
- package/src/code-tool.ts +112 -0
- package/src/docs-search-tool.ts +59 -0
- package/src/headers.ts +27 -0
- package/src/http.ts +107 -0
- package/src/index.ts +61 -0
- package/src/options.ts +93 -0
- package/src/server.ts +154 -0
- package/src/stdio.ts +12 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +115 -0
- package/stdio.d.mts +2 -0
- package/stdio.d.mts.map +1 -0
- package/stdio.d.ts +2 -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
|
@@ -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/believe/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,27 @@
|
|
|
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 '@cjavdev/believe';
|
|
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 'Bearer':
|
|
12
|
+
return { apiKey: req.headers.authorization.slice('Bearer '.length) };
|
|
13
|
+
default:
|
|
14
|
+
throw new Error(
|
|
15
|
+
'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Bearer).',
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
} else if (required) {
|
|
19
|
+
throw new Error('Missing required Authorization header; see WWW-Authenticate header for details.');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const apiKey =
|
|
23
|
+
Array.isArray(req.headers['x-believe-api-key']) ?
|
|
24
|
+
req.headers['x-believe-api-key'][0]
|
|
25
|
+
: req.headers['x-believe-api-key'];
|
|
26
|
+
return { apiKey };
|
|
27
|
+
};
|
package/src/http.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
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 { McpOptions } from './options';
|
|
8
|
+
import { ClientOptions, initMcpServer, newMcpServer } from './server';
|
|
9
|
+
import { parseAuthHeaders } from './headers';
|
|
10
|
+
|
|
11
|
+
const newServer = ({
|
|
12
|
+
clientOptions,
|
|
13
|
+
req,
|
|
14
|
+
res,
|
|
15
|
+
}: {
|
|
16
|
+
clientOptions: ClientOptions;
|
|
17
|
+
req: express.Request;
|
|
18
|
+
res: express.Response;
|
|
19
|
+
}): McpServer | null => {
|
|
20
|
+
const server = newMcpServer();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const authOptions = parseAuthHeaders(req, false);
|
|
24
|
+
initMcpServer({
|
|
25
|
+
server: server,
|
|
26
|
+
clientOptions: {
|
|
27
|
+
...clientOptions,
|
|
28
|
+
...authOptions,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
} catch (error) {
|
|
32
|
+
res.status(401).json({
|
|
33
|
+
jsonrpc: '2.0',
|
|
34
|
+
error: {
|
|
35
|
+
code: -32000,
|
|
36
|
+
message: `Unauthorized: ${error instanceof Error ? error.message : error}`,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return server;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const post =
|
|
46
|
+
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
|
|
47
|
+
async (req: express.Request, res: express.Response) => {
|
|
48
|
+
const server = newServer({ ...options, req, res });
|
|
49
|
+
// If we return null, we already set the authorization error.
|
|
50
|
+
if (server === null) return;
|
|
51
|
+
const transport = new StreamableHTTPServerTransport();
|
|
52
|
+
await server.connect(transport as any);
|
|
53
|
+
await transport.handleRequest(req, res, req.body);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const get = async (req: express.Request, res: express.Response) => {
|
|
57
|
+
res.status(405).json({
|
|
58
|
+
jsonrpc: '2.0',
|
|
59
|
+
error: {
|
|
60
|
+
code: -32000,
|
|
61
|
+
message: 'Method not supported',
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const del = async (req: express.Request, res: express.Response) => {
|
|
67
|
+
res.status(405).json({
|
|
68
|
+
jsonrpc: '2.0',
|
|
69
|
+
error: {
|
|
70
|
+
code: -32000,
|
|
71
|
+
message: 'Method not supported',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const streamableHTTPApp = ({
|
|
77
|
+
clientOptions = {},
|
|
78
|
+
mcpOptions,
|
|
79
|
+
}: {
|
|
80
|
+
clientOptions?: ClientOptions;
|
|
81
|
+
mcpOptions: McpOptions;
|
|
82
|
+
}): express.Express => {
|
|
83
|
+
const app = express();
|
|
84
|
+
app.set('query parser', 'extended');
|
|
85
|
+
app.use(express.json());
|
|
86
|
+
app.use(morgan('combined'));
|
|
87
|
+
|
|
88
|
+
app.get('/', get);
|
|
89
|
+
app.post('/', post({ clientOptions, mcpOptions }));
|
|
90
|
+
app.delete('/', del);
|
|
91
|
+
|
|
92
|
+
return app;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const launchStreamableHTTPServer = async (options: McpOptions, port: number | string | undefined) => {
|
|
96
|
+
const app = streamableHTTPApp({ mcpOptions: options });
|
|
97
|
+
const server = app.listen(port);
|
|
98
|
+
const address = server.address();
|
|
99
|
+
|
|
100
|
+
if (typeof address === 'string') {
|
|
101
|
+
console.error(`MCP Server running on streamable HTTP at ${address}`);
|
|
102
|
+
} else if (address !== null) {
|
|
103
|
+
console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
|
|
104
|
+
} else {
|
|
105
|
+
console.error(`MCP Server running on streamable HTTP on port ${port}`);
|
|
106
|
+
}
|
|
107
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
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();
|
|
22
|
+
break;
|
|
23
|
+
case 'http':
|
|
24
|
+
await launchStreamableHTTPServer(options, options.port ?? options.socket);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (require.main === module) {
|
|
30
|
+
main().catch((error) => {
|
|
31
|
+
console.error('Fatal error in main():', error);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseOptionsOrError() {
|
|
37
|
+
try {
|
|
38
|
+
return parseCLIOptions();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('Error parsing options:', error);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
|
|
46
|
+
try {
|
|
47
|
+
const includedTools = selectTools(options);
|
|
48
|
+
if (includedTools.length === 0) {
|
|
49
|
+
console.error('No tools match the provided filters.');
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
return includedTools;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
console.error('Error filtering tools:', error.message);
|
|
56
|
+
} else {
|
|
57
|
+
console.error('Error filtering tools:', error);
|
|
58
|
+
}
|
|
59
|
+
process.exit(1);
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/options.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import z from 'zod';
|
|
5
|
+
|
|
6
|
+
export type CLIOptions = McpOptions & {
|
|
7
|
+
transport: 'stdio' | 'http';
|
|
8
|
+
port: number | undefined;
|
|
9
|
+
socket: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type McpOptions = {
|
|
13
|
+
includeDocsTools?: boolean | undefined;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function parseCLIOptions(): CLIOptions {
|
|
17
|
+
const opts = yargs(hideBin(process.argv))
|
|
18
|
+
.option('tools', {
|
|
19
|
+
type: 'string',
|
|
20
|
+
array: true,
|
|
21
|
+
choices: ['code', 'docs'],
|
|
22
|
+
description: 'Use dynamic tools or all tools',
|
|
23
|
+
})
|
|
24
|
+
.option('no-tools', {
|
|
25
|
+
type: 'string',
|
|
26
|
+
array: true,
|
|
27
|
+
choices: ['code', 'docs'],
|
|
28
|
+
description: 'Do not use any dynamic or all tools',
|
|
29
|
+
})
|
|
30
|
+
.option('transport', {
|
|
31
|
+
type: 'string',
|
|
32
|
+
choices: ['stdio', 'http'],
|
|
33
|
+
default: 'stdio',
|
|
34
|
+
description: 'What transport to use; stdio for local servers or http for remote servers',
|
|
35
|
+
})
|
|
36
|
+
.option('port', {
|
|
37
|
+
type: 'number',
|
|
38
|
+
default: 3000,
|
|
39
|
+
description: 'Port to serve on if using http transport',
|
|
40
|
+
})
|
|
41
|
+
.option('socket', {
|
|
42
|
+
type: 'string',
|
|
43
|
+
description: 'Unix socket to serve on if using http transport',
|
|
44
|
+
})
|
|
45
|
+
.help();
|
|
46
|
+
|
|
47
|
+
const argv = opts.parseSync();
|
|
48
|
+
|
|
49
|
+
const shouldIncludeToolType = (toolType: 'code' | 'docs') =>
|
|
50
|
+
argv.noTools?.includes(toolType) ? false
|
|
51
|
+
: argv.tools?.includes(toolType) ? true
|
|
52
|
+
: undefined;
|
|
53
|
+
|
|
54
|
+
const includeDocsTools = shouldIncludeToolType('docs');
|
|
55
|
+
|
|
56
|
+
const transport = argv.transport as 'stdio' | 'http';
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
...(includeDocsTools !== undefined && { includeDocsTools }),
|
|
60
|
+
transport,
|
|
61
|
+
port: argv.port,
|
|
62
|
+
socket: argv.socket,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
|
|
67
|
+
z.preprocess(
|
|
68
|
+
(val) =>
|
|
69
|
+
Array.isArray(val) ? val
|
|
70
|
+
: val ? [val]
|
|
71
|
+
: val,
|
|
72
|
+
z.array(zodType).optional(),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const QueryOptions = z.object({
|
|
76
|
+
tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to use'),
|
|
77
|
+
no_tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to not use.'),
|
|
78
|
+
tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): McpOptions {
|
|
82
|
+
const queryObject = typeof query === 'string' ? qs.parse(query) : query;
|
|
83
|
+
const queryOptions = QueryOptions.parse(queryObject);
|
|
84
|
+
|
|
85
|
+
let docsTools: boolean | undefined =
|
|
86
|
+
queryOptions.no_tools && queryOptions.no_tools?.includes('docs') ? false
|
|
87
|
+
: queryOptions.tools?.includes('docs') ? true
|
|
88
|
+
: defaultOptions.includeDocsTools;
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
...(docsTools !== undefined && { includeDocsTools: docsTools }),
|
|
92
|
+
};
|
|
93
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema,
|
|
8
|
+
SetLevelRequestSchema,
|
|
9
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import { ClientOptions } from '@cjavdev/believe';
|
|
11
|
+
import Believe from '@cjavdev/believe';
|
|
12
|
+
import { codeTool } from './code-tool';
|
|
13
|
+
import docsSearchTool from './docs-search-tool';
|
|
14
|
+
import { McpOptions } from './options';
|
|
15
|
+
import { HandlerFunction, McpTool } from './types';
|
|
16
|
+
|
|
17
|
+
export { McpOptions } from './options';
|
|
18
|
+
export { ClientOptions } from '@cjavdev/believe';
|
|
19
|
+
|
|
20
|
+
export const newMcpServer = () =>
|
|
21
|
+
new McpServer(
|
|
22
|
+
{
|
|
23
|
+
name: 'cjavdev_believe_api',
|
|
24
|
+
version: '0.5.1',
|
|
25
|
+
},
|
|
26
|
+
{ capabilities: { tools: {}, logging: {} } },
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
// Create server instance
|
|
30
|
+
export const server = newMcpServer();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
34
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
35
|
+
*/
|
|
36
|
+
export function initMcpServer(params: {
|
|
37
|
+
server: Server | McpServer;
|
|
38
|
+
clientOptions?: ClientOptions;
|
|
39
|
+
mcpOptions?: McpOptions;
|
|
40
|
+
}) {
|
|
41
|
+
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
42
|
+
|
|
43
|
+
const logAtLevel =
|
|
44
|
+
(level: 'debug' | 'info' | 'warning' | 'error') =>
|
|
45
|
+
(message: string, ...rest: unknown[]) => {
|
|
46
|
+
void server.sendLoggingMessage({
|
|
47
|
+
level,
|
|
48
|
+
data: { message, rest },
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const logger = {
|
|
52
|
+
debug: logAtLevel('debug'),
|
|
53
|
+
info: logAtLevel('info'),
|
|
54
|
+
warn: logAtLevel('warning'),
|
|
55
|
+
error: logAtLevel('error'),
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let client = new Believe({
|
|
59
|
+
logger,
|
|
60
|
+
...params.clientOptions,
|
|
61
|
+
defaultHeaders: {
|
|
62
|
+
...params.clientOptions?.defaultHeaders,
|
|
63
|
+
'X-Stainless-MCP': 'true',
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const providedTools = selectTools(params.mcpOptions);
|
|
68
|
+
const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
|
|
69
|
+
|
|
70
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
71
|
+
return {
|
|
72
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
77
|
+
const { name, arguments: args } = request.params;
|
|
78
|
+
const mcpTool = toolMap[name];
|
|
79
|
+
if (!mcpTool) {
|
|
80
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return executeHandler(mcpTool.handler, client, args);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
87
|
+
const { level } = request.params;
|
|
88
|
+
switch (level) {
|
|
89
|
+
case 'debug':
|
|
90
|
+
client = client.withOptions({ logLevel: 'debug' });
|
|
91
|
+
break;
|
|
92
|
+
case 'info':
|
|
93
|
+
client = client.withOptions({ logLevel: 'info' });
|
|
94
|
+
break;
|
|
95
|
+
case 'notice':
|
|
96
|
+
case 'warning':
|
|
97
|
+
client = client.withOptions({ logLevel: 'warn' });
|
|
98
|
+
break;
|
|
99
|
+
case 'error':
|
|
100
|
+
client = client.withOptions({ logLevel: 'error' });
|
|
101
|
+
break;
|
|
102
|
+
default:
|
|
103
|
+
client = client.withOptions({ logLevel: 'off' });
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
return {};
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
112
|
+
*/
|
|
113
|
+
export function selectTools(options?: McpOptions): McpTool[] {
|
|
114
|
+
const includedTools = [codeTool()];
|
|
115
|
+
if (options?.includeDocsTools ?? true) {
|
|
116
|
+
includedTools.push(docsSearchTool);
|
|
117
|
+
}
|
|
118
|
+
return includedTools;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Runs the provided handler with the given client and arguments.
|
|
123
|
+
*/
|
|
124
|
+
export async function executeHandler(
|
|
125
|
+
handler: HandlerFunction,
|
|
126
|
+
client: Believe,
|
|
127
|
+
args: Record<string, unknown> | undefined,
|
|
128
|
+
) {
|
|
129
|
+
return await handler(client, args || {});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const readEnv = (env: string): string | undefined => {
|
|
133
|
+
if (typeof (globalThis as any).process !== 'undefined') {
|
|
134
|
+
return (globalThis as any).process.env?.[env]?.trim();
|
|
135
|
+
} else if (typeof (globalThis as any).Deno !== 'undefined') {
|
|
136
|
+
return (globalThis as any).Deno.env?.get?.(env)?.trim();
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const readEnvOrError = (env: string): string => {
|
|
142
|
+
let envValue = readEnv(env);
|
|
143
|
+
if (envValue === undefined) {
|
|
144
|
+
throw new Error(`Environment variable ${env} is not set`);
|
|
145
|
+
}
|
|
146
|
+
return envValue;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const requireValue = <T>(value: T | undefined, description: string): T => {
|
|
150
|
+
if (value === undefined) {
|
|
151
|
+
throw new Error(`Missing required value: ${description}`);
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
};
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
3
|
+
|
|
4
|
+
export const launchStdioServer = async () => {
|
|
5
|
+
const server = newMcpServer();
|
|
6
|
+
|
|
7
|
+
initMcpServer({ server });
|
|
8
|
+
|
|
9
|
+
const transport = new StdioServerTransport();
|
|
10
|
+
await server.connect(transport);
|
|
11
|
+
console.error('MCP Server running on stdio');
|
|
12
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
// this config is included in the published src directory to prevent TS errors
|
|
3
|
+
// from appearing when users go to source, and VSCode opens the source .ts file
|
|
4
|
+
// via declaration maps
|
|
5
|
+
"include": ["index.ts"],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"target": "es2015",
|
|
8
|
+
"lib": ["DOM"],
|
|
9
|
+
"moduleResolution": "node"
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import Believe from '@cjavdev/believe';
|
|
4
|
+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
|
|
6
|
+
type TextContentBlock = {
|
|
7
|
+
type: 'text';
|
|
8
|
+
text: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type ImageContentBlock = {
|
|
12
|
+
type: 'image';
|
|
13
|
+
data: string;
|
|
14
|
+
mimeType: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type AudioContentBlock = {
|
|
18
|
+
type: 'audio';
|
|
19
|
+
data: string;
|
|
20
|
+
mimeType: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type ResourceContentBlock = {
|
|
24
|
+
type: 'resource';
|
|
25
|
+
resource:
|
|
26
|
+
| {
|
|
27
|
+
uri: string;
|
|
28
|
+
mimeType: string;
|
|
29
|
+
text: string;
|
|
30
|
+
}
|
|
31
|
+
| {
|
|
32
|
+
uri: string;
|
|
33
|
+
mimeType: string;
|
|
34
|
+
blob: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type ContentBlock = TextContentBlock | ImageContentBlock | AudioContentBlock | ResourceContentBlock;
|
|
39
|
+
|
|
40
|
+
export type ToolCallResult = {
|
|
41
|
+
content: ContentBlock[];
|
|
42
|
+
isError?: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type HandlerFunction = (
|
|
46
|
+
client: Believe,
|
|
47
|
+
args: Record<string, unknown> | undefined,
|
|
48
|
+
) => Promise<ToolCallResult>;
|
|
49
|
+
|
|
50
|
+
export function asTextContentResult(result: unknown): ToolCallResult {
|
|
51
|
+
return {
|
|
52
|
+
content: [
|
|
53
|
+
{
|
|
54
|
+
type: 'text',
|
|
55
|
+
text: JSON.stringify(result, null, 2),
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function asBinaryContentResult(response: Response): Promise<ToolCallResult> {
|
|
62
|
+
const blob = await response.blob();
|
|
63
|
+
const mimeType = blob.type;
|
|
64
|
+
const data = Buffer.from(await blob.arrayBuffer()).toString('base64');
|
|
65
|
+
if (mimeType.startsWith('image/')) {
|
|
66
|
+
return {
|
|
67
|
+
content: [{ type: 'image', mimeType, data }],
|
|
68
|
+
};
|
|
69
|
+
} else if (mimeType.startsWith('audio/')) {
|
|
70
|
+
return {
|
|
71
|
+
content: [{ type: 'audio', mimeType, data }],
|
|
72
|
+
};
|
|
73
|
+
} else {
|
|
74
|
+
return {
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: 'resource',
|
|
78
|
+
resource: {
|
|
79
|
+
// We must give a URI, even though this isn't actually an MCP resource.
|
|
80
|
+
uri: 'resource://tool-response',
|
|
81
|
+
mimeType,
|
|
82
|
+
blob: data,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function asErrorResult(message: string): ToolCallResult {
|
|
91
|
+
return {
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
type: 'text',
|
|
95
|
+
text: message,
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
isError: true,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type Metadata = {
|
|
103
|
+
resource: string;
|
|
104
|
+
operation: 'read' | 'write';
|
|
105
|
+
tags: string[];
|
|
106
|
+
httpMethod?: string;
|
|
107
|
+
httpPath?: string;
|
|
108
|
+
operationId?: string;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export type McpTool = {
|
|
112
|
+
metadata: Metadata;
|
|
113
|
+
tool: Tool;
|
|
114
|
+
handler: HandlerFunction;
|
|
115
|
+
};
|
package/stdio.d.mts
ADDED
package/stdio.d.mts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.d.mts","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,iBAAiB,qBAQ7B,CAAC"}
|
package/stdio.d.ts
ADDED
package/stdio.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.d.ts","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,iBAAiB,qBAQ7B,CAAC"}
|
package/stdio.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.launchStdioServer = void 0;
|
|
4
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
5
|
+
const server_1 = require("./server.js");
|
|
6
|
+
const launchStdioServer = async () => {
|
|
7
|
+
const server = (0, server_1.newMcpServer)();
|
|
8
|
+
(0, server_1.initMcpServer)({ server });
|
|
9
|
+
const transport = new stdio_js_1.StdioServerTransport();
|
|
10
|
+
await server.connect(transport);
|
|
11
|
+
console.error('MCP Server running on stdio');
|
|
12
|
+
};
|
|
13
|
+
exports.launchStdioServer = launchStdioServer;
|
|
14
|
+
//# sourceMappingURL=stdio.js.map
|
package/stdio.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.js","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":";;;AAAA,wEAAiF;AACjF,wCAAuD;AAEhD,MAAM,iBAAiB,GAAG,KAAK,IAAI,EAAE;IAC1C,MAAM,MAAM,GAAG,IAAA,qBAAY,GAAE,CAAC;IAE9B,IAAA,sBAAa,EAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IAE1B,MAAM,SAAS,GAAG,IAAI,+BAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;AAC/C,CAAC,CAAC;AARW,QAAA,iBAAiB,qBAQ5B"}
|
package/stdio.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { initMcpServer, newMcpServer } from "./server.mjs";
|
|
3
|
+
export const launchStdioServer = async () => {
|
|
4
|
+
const server = newMcpServer();
|
|
5
|
+
initMcpServer({ server });
|
|
6
|
+
const transport = new StdioServerTransport();
|
|
7
|
+
await server.connect(transport);
|
|
8
|
+
console.error('MCP Server running on stdio');
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=stdio.mjs.map
|
package/stdio.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio.mjs","sourceRoot":"","sources":["src/stdio.ts"],"names":[],"mappings":"OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C;OACzE,EAAE,aAAa,EAAE,YAAY,EAAE;AAEtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,IAAI,EAAE;IAC1C,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC;IAE9B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;IAE1B,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;AAC/C,CAAC,CAAC"}
|