@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.
Files changed (94) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +101 -0
  3. package/code-tool-types.d.mts +14 -0
  4. package/code-tool-types.d.mts.map +1 -0
  5. package/code-tool-types.d.ts +14 -0
  6. package/code-tool-types.d.ts.map +1 -0
  7. package/code-tool-types.js +4 -0
  8. package/code-tool-types.js.map +1 -0
  9. package/code-tool-types.mjs +3 -0
  10. package/code-tool-types.mjs.map +1 -0
  11. package/code-tool.d.mts +12 -0
  12. package/code-tool.d.mts.map +1 -0
  13. package/code-tool.d.ts +12 -0
  14. package/code-tool.d.ts.map +1 -0
  15. package/code-tool.js +97 -0
  16. package/code-tool.js.map +1 -0
  17. package/code-tool.mjs +94 -0
  18. package/code-tool.mjs.map +1 -0
  19. package/docs-search-tool.d.mts +51 -0
  20. package/docs-search-tool.d.mts.map +1 -0
  21. package/docs-search-tool.d.ts +51 -0
  22. package/docs-search-tool.d.ts.map +1 -0
  23. package/docs-search-tool.js +51 -0
  24. package/docs-search-tool.js.map +1 -0
  25. package/docs-search-tool.mjs +47 -0
  26. package/docs-search-tool.mjs.map +1 -0
  27. package/headers.d.mts +4 -0
  28. package/headers.d.mts.map +1 -0
  29. package/headers.d.ts +4 -0
  30. package/headers.d.ts.map +1 -0
  31. package/headers.js +25 -0
  32. package/headers.js.map +1 -0
  33. package/headers.mjs +21 -0
  34. package/headers.mjs.map +1 -0
  35. package/http.d.mts +9 -0
  36. package/http.d.mts.map +1 -0
  37. package/http.d.ts +9 -0
  38. package/http.d.ts.map +1 -0
  39. package/http.js +90 -0
  40. package/http.js.map +1 -0
  41. package/http.mjs +82 -0
  42. package/http.mjs.map +1 -0
  43. package/index.d.mts +3 -0
  44. package/index.d.mts.map +1 -0
  45. package/index.d.ts +3 -0
  46. package/index.d.ts.map +1 -0
  47. package/index.js +55 -0
  48. package/index.js.map +1 -0
  49. package/index.mjs +53 -0
  50. package/index.mjs.map +1 -0
  51. package/options.d.mts +11 -0
  52. package/options.d.mts.map +1 -0
  53. package/options.d.ts +11 -0
  54. package/options.d.ts.map +1 -0
  55. package/options.js +73 -0
  56. package/options.js.map +1 -0
  57. package/options.mjs +66 -0
  58. package/options.mjs.map +1 -0
  59. package/package.json +163 -0
  60. package/server.d.mts +31 -0
  61. package/server.d.mts.map +1 -0
  62. package/server.d.ts +31 -0
  63. package/server.d.ts.map +1 -0
  64. package/server.js +128 -0
  65. package/server.js.map +1 -0
  66. package/server.mjs +115 -0
  67. package/server.mjs.map +1 -0
  68. package/src/code-tool-types.ts +16 -0
  69. package/src/code-tool.ts +112 -0
  70. package/src/docs-search-tool.ts +59 -0
  71. package/src/headers.ts +27 -0
  72. package/src/http.ts +107 -0
  73. package/src/index.ts +61 -0
  74. package/src/options.ts +93 -0
  75. package/src/server.ts +154 -0
  76. package/src/stdio.ts +12 -0
  77. package/src/tsconfig.json +11 -0
  78. package/src/types.ts +115 -0
  79. package/stdio.d.mts +2 -0
  80. package/stdio.d.mts.map +1 -0
  81. package/stdio.d.ts +2 -0
  82. package/stdio.d.ts.map +1 -0
  83. package/stdio.js +14 -0
  84. package/stdio.js.map +1 -0
  85. package/stdio.mjs +10 -0
  86. package/stdio.mjs.map +1 -0
  87. package/types.d.mts +52 -0
  88. package/types.d.mts.map +1 -0
  89. package/types.d.ts +52 -0
  90. package/types.d.ts.map +1 -0
  91. package/types.js +58 -0
  92. package/types.js.map +1 -0
  93. package/types.mjs +53 -0
  94. package/types.mjs.map +1 -0
package/package.json ADDED
@@ -0,0 +1,163 @@
1
+ {
2
+ "name": "@cjavdev/believe-mcp",
3
+ "version": "0.5.1",
4
+ "description": "The official MCP Server for the Believe API",
5
+ "author": "Believe <wave@cjav.dev>",
6
+ "types": "./index.d.ts",
7
+ "main": "./index.js",
8
+ "type": "commonjs",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/cjavdev/believe-typescript.git",
12
+ "directory": "packages/mcp-server"
13
+ },
14
+ "homepage": "https://github.com/cjavdev/believe-typescript/tree/main/packages/mcp-server#readme",
15
+ "license": "MIT",
16
+ "private": false,
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "dependencies": {
21
+ "@cjavdev/believe": "^0.5.1",
22
+ "@cloudflare/cabidela": "^0.2.4",
23
+ "@modelcontextprotocol/sdk": "^1.25.2",
24
+ "@valtown/deno-http-worker": "^0.0.21",
25
+ "cookie-parser": "^1.4.6",
26
+ "cors": "^2.8.5",
27
+ "express": "^5.1.0",
28
+ "fuse.js": "^7.1.0",
29
+ "jq-web": "https://github.com/stainless-api/jq-web/releases/download/v0.8.8/jq-web.tar.gz",
30
+ "morgan": "^1.10.0",
31
+ "qs": "^6.14.1",
32
+ "typescript": "5.8.3",
33
+ "yargs": "^17.7.2",
34
+ "zod": "^3.25.20",
35
+ "zod-to-json-schema": "^3.24.5",
36
+ "zod-validation-error": "^4.0.1"
37
+ },
38
+ "bin": {
39
+ "mcp-server": "./index.js"
40
+ },
41
+ "imports": {
42
+ "@cjavdev/believe-mcp": ".",
43
+ "@cjavdev/believe-mcp/*": "./src/*"
44
+ },
45
+ "exports": {
46
+ ".": {
47
+ "require": {
48
+ "types": "./index.d.ts",
49
+ "default": "./index.js"
50
+ },
51
+ "types": "./index.d.mts",
52
+ "default": "./index.mjs"
53
+ },
54
+ "./code-tool-types": {
55
+ "import": "./code-tool-types.mjs",
56
+ "require": "./code-tool-types.js"
57
+ },
58
+ "./code-tool-types.js": {
59
+ "default": "./code-tool-types.js"
60
+ },
61
+ "./code-tool-types.mjs": {
62
+ "default": "./code-tool-types.mjs"
63
+ },
64
+ "./code-tool": {
65
+ "import": "./code-tool.mjs",
66
+ "require": "./code-tool.js"
67
+ },
68
+ "./code-tool.js": {
69
+ "default": "./code-tool.js"
70
+ },
71
+ "./code-tool.mjs": {
72
+ "default": "./code-tool.mjs"
73
+ },
74
+ "./docs-search-tool": {
75
+ "import": "./docs-search-tool.mjs",
76
+ "require": "./docs-search-tool.js"
77
+ },
78
+ "./docs-search-tool.js": {
79
+ "default": "./docs-search-tool.js"
80
+ },
81
+ "./docs-search-tool.mjs": {
82
+ "default": "./docs-search-tool.mjs"
83
+ },
84
+ "./headers": {
85
+ "import": "./headers.mjs",
86
+ "require": "./headers.js"
87
+ },
88
+ "./headers.js": {
89
+ "default": "./headers.js"
90
+ },
91
+ "./headers.mjs": {
92
+ "default": "./headers.mjs"
93
+ },
94
+ "./http": {
95
+ "import": "./http.mjs",
96
+ "require": "./http.js"
97
+ },
98
+ "./http.js": {
99
+ "default": "./http.js"
100
+ },
101
+ "./http.mjs": {
102
+ "default": "./http.mjs"
103
+ },
104
+ "./index": {
105
+ "import": "./index.mjs",
106
+ "require": "./index.js"
107
+ },
108
+ "./index.js": {
109
+ "default": "./index.js"
110
+ },
111
+ "./index.mjs": {
112
+ "default": "./index.mjs"
113
+ },
114
+ "./options": {
115
+ "import": "./options.mjs",
116
+ "require": "./options.js"
117
+ },
118
+ "./options.js": {
119
+ "default": "./options.js"
120
+ },
121
+ "./options.mjs": {
122
+ "default": "./options.mjs"
123
+ },
124
+ "./server": {
125
+ "import": "./server.mjs",
126
+ "require": "./server.js"
127
+ },
128
+ "./server.js": {
129
+ "default": "./server.js"
130
+ },
131
+ "./server.mjs": {
132
+ "default": "./server.mjs"
133
+ },
134
+ "./stdio": {
135
+ "import": "./stdio.mjs",
136
+ "require": "./stdio.js"
137
+ },
138
+ "./stdio.js": {
139
+ "default": "./stdio.js"
140
+ },
141
+ "./stdio.mjs": {
142
+ "default": "./stdio.mjs"
143
+ },
144
+ "./types": {
145
+ "import": "./types.mjs",
146
+ "require": "./types.js"
147
+ },
148
+ "./types.js": {
149
+ "default": "./types.js"
150
+ },
151
+ "./types.mjs": {
152
+ "default": "./types.mjs"
153
+ }
154
+ },
155
+ "scripts": {
156
+ "test": "jest",
157
+ "build": "bash ./build",
158
+ "format": "prettier --write --cache --cache-strategy metadata . !dist",
159
+ "tsn": "ts-node -r tsconfig-paths/register",
160
+ "lint": "eslint --ext ts,js .",
161
+ "fix": "eslint --fix --ext ts,js ."
162
+ }
163
+ }
package/server.d.mts ADDED
@@ -0,0 +1,31 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { ClientOptions } from '@cjavdev/believe';
4
+ import Believe from '@cjavdev/believe';
5
+ import { McpOptions } from "./options.mjs";
6
+ import { HandlerFunction, McpTool } from "./types.mjs";
7
+ export { McpOptions } from "./options.mjs";
8
+ export { ClientOptions } from '@cjavdev/believe';
9
+ export declare const newMcpServer: () => McpServer;
10
+ export declare const server: McpServer;
11
+ /**
12
+ * Initializes the provided MCP Server with the given tools and handlers.
13
+ * If not provided, the default client, tools and handlers will be used.
14
+ */
15
+ export declare function initMcpServer(params: {
16
+ server: Server | McpServer;
17
+ clientOptions?: ClientOptions;
18
+ mcpOptions?: McpOptions;
19
+ }): void;
20
+ /**
21
+ * Selects the tools to include in the MCP Server based on the provided options.
22
+ */
23
+ export declare function selectTools(options?: McpOptions): McpTool[];
24
+ /**
25
+ * Runs the provided handler with the given client and arguments.
26
+ */
27
+ export declare function executeHandler(handler: HandlerFunction, client: Believe, args: Record<string, unknown> | undefined): Promise<import("./types").ToolCallResult>;
28
+ export declare const readEnv: (env: string) => string | undefined;
29
+ export declare const readEnvOrError: (env: string) => string;
30
+ export declare const requireValue: <T>(value: T | undefined, description: string) => T;
31
+ //# sourceMappingURL=server.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.mts","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"OAEO,EAAE,MAAM,EAAE,MAAM,2CAA2C;OAC3D,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAM5D,EAAE,aAAa,EAAE,MAAM,kBAAkB;OACzC,OAAO,MAAM,kBAAkB;OAG/B,EAAE,UAAU,EAAE;OACd,EAAE,eAAe,EAAE,OAAO,EAAE;OAE5B,EAAE,UAAU,EAAE;OACd,EAAE,aAAa,EAAE,MAAM,kBAAkB;AAEhD,eAAO,MAAM,YAAY,iBAOtB,CAAC;AAGJ,eAAO,MAAM,MAAM,WAAiB,CAAC;AAErC;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE;IACpC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,QAoEA;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,EAAE,CAM3D;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,OAAO,EACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,6CAG1C;AAED,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAO9C,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,KAAK,MAAM,KAAG,MAM5C,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,CAAC,EAAE,OAAO,CAAC,GAAG,SAAS,EAAE,aAAa,MAAM,KAAG,CAK3E,CAAC"}
package/server.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { ClientOptions } from '@cjavdev/believe';
4
+ import Believe from '@cjavdev/believe';
5
+ import { McpOptions } from "./options.js";
6
+ import { HandlerFunction, McpTool } from "./types.js";
7
+ export { McpOptions } from "./options.js";
8
+ export { ClientOptions } from '@cjavdev/believe';
9
+ export declare const newMcpServer: () => McpServer;
10
+ export declare const server: McpServer;
11
+ /**
12
+ * Initializes the provided MCP Server with the given tools and handlers.
13
+ * If not provided, the default client, tools and handlers will be used.
14
+ */
15
+ export declare function initMcpServer(params: {
16
+ server: Server | McpServer;
17
+ clientOptions?: ClientOptions;
18
+ mcpOptions?: McpOptions;
19
+ }): void;
20
+ /**
21
+ * Selects the tools to include in the MCP Server based on the provided options.
22
+ */
23
+ export declare function selectTools(options?: McpOptions): McpTool[];
24
+ /**
25
+ * Runs the provided handler with the given client and arguments.
26
+ */
27
+ export declare function executeHandler(handler: HandlerFunction, client: Believe, args: Record<string, unknown> | undefined): Promise<import("./types").ToolCallResult>;
28
+ export declare const readEnv: (env: string) => string | undefined;
29
+ export declare const readEnvOrError: (env: string) => string;
30
+ export declare const requireValue: <T>(value: T | undefined, description: string) => T;
31
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"OAEO,EAAE,MAAM,EAAE,MAAM,2CAA2C;OAC3D,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAM5D,EAAE,aAAa,EAAE,MAAM,kBAAkB;OACzC,OAAO,MAAM,kBAAkB;OAG/B,EAAE,UAAU,EAAE;OACd,EAAE,eAAe,EAAE,OAAO,EAAE;OAE5B,EAAE,UAAU,EAAE;OACd,EAAE,aAAa,EAAE,MAAM,kBAAkB;AAEhD,eAAO,MAAM,YAAY,iBAOtB,CAAC;AAGJ,eAAO,MAAM,MAAM,WAAiB,CAAC;AAErC;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE;IACpC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,QAoEA;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,EAAE,CAM3D;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,OAAO,EACf,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,6CAG1C;AAED,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAO9C,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,KAAK,MAAM,KAAG,MAM5C,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,CAAC,EAAE,OAAO,CAAC,GAAG,SAAS,EAAE,aAAa,MAAM,KAAG,CAK3E,CAAC"}
package/server.js ADDED
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.requireValue = exports.readEnvOrError = exports.readEnv = exports.server = exports.newMcpServer = void 0;
8
+ exports.initMcpServer = initMcpServer;
9
+ exports.selectTools = selectTools;
10
+ exports.executeHandler = executeHandler;
11
+ const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
12
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
13
+ const believe_1 = __importDefault(require("@cjavdev/believe"));
14
+ const code_tool_1 = require("./code-tool.js");
15
+ const docs_search_tool_1 = __importDefault(require("./docs-search-tool.js"));
16
+ const newMcpServer = () => new mcp_js_1.McpServer({
17
+ name: 'cjavdev_believe_api',
18
+ version: '0.5.1',
19
+ }, { capabilities: { tools: {}, logging: {} } });
20
+ exports.newMcpServer = newMcpServer;
21
+ // Create server instance
22
+ exports.server = (0, exports.newMcpServer)();
23
+ /**
24
+ * Initializes the provided MCP Server with the given tools and handlers.
25
+ * If not provided, the default client, tools and handlers will be used.
26
+ */
27
+ function initMcpServer(params) {
28
+ const server = params.server instanceof mcp_js_1.McpServer ? params.server.server : params.server;
29
+ const logAtLevel = (level) => (message, ...rest) => {
30
+ void server.sendLoggingMessage({
31
+ level,
32
+ data: { message, rest },
33
+ });
34
+ };
35
+ const logger = {
36
+ debug: logAtLevel('debug'),
37
+ info: logAtLevel('info'),
38
+ warn: logAtLevel('warning'),
39
+ error: logAtLevel('error'),
40
+ };
41
+ let client = new believe_1.default({
42
+ logger,
43
+ ...params.clientOptions,
44
+ defaultHeaders: {
45
+ ...params.clientOptions?.defaultHeaders,
46
+ 'X-Stainless-MCP': 'true',
47
+ },
48
+ });
49
+ const providedTools = selectTools(params.mcpOptions);
50
+ const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
51
+ server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => {
52
+ return {
53
+ tools: providedTools.map((mcpTool) => mcpTool.tool),
54
+ };
55
+ });
56
+ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
57
+ const { name, arguments: args } = request.params;
58
+ const mcpTool = toolMap[name];
59
+ if (!mcpTool) {
60
+ throw new Error(`Unknown tool: ${name}`);
61
+ }
62
+ return executeHandler(mcpTool.handler, client, args);
63
+ });
64
+ server.setRequestHandler(types_js_1.SetLevelRequestSchema, async (request) => {
65
+ const { level } = request.params;
66
+ switch (level) {
67
+ case 'debug':
68
+ client = client.withOptions({ logLevel: 'debug' });
69
+ break;
70
+ case 'info':
71
+ client = client.withOptions({ logLevel: 'info' });
72
+ break;
73
+ case 'notice':
74
+ case 'warning':
75
+ client = client.withOptions({ logLevel: 'warn' });
76
+ break;
77
+ case 'error':
78
+ client = client.withOptions({ logLevel: 'error' });
79
+ break;
80
+ default:
81
+ client = client.withOptions({ logLevel: 'off' });
82
+ break;
83
+ }
84
+ return {};
85
+ });
86
+ }
87
+ /**
88
+ * Selects the tools to include in the MCP Server based on the provided options.
89
+ */
90
+ function selectTools(options) {
91
+ const includedTools = [(0, code_tool_1.codeTool)()];
92
+ if (options?.includeDocsTools ?? true) {
93
+ includedTools.push(docs_search_tool_1.default);
94
+ }
95
+ return includedTools;
96
+ }
97
+ /**
98
+ * Runs the provided handler with the given client and arguments.
99
+ */
100
+ async function executeHandler(handler, client, args) {
101
+ return await handler(client, args || {});
102
+ }
103
+ const readEnv = (env) => {
104
+ if (typeof globalThis.process !== 'undefined') {
105
+ return globalThis.process.env?.[env]?.trim();
106
+ }
107
+ else if (typeof globalThis.Deno !== 'undefined') {
108
+ return globalThis.Deno.env?.get?.(env)?.trim();
109
+ }
110
+ return;
111
+ };
112
+ exports.readEnv = readEnv;
113
+ const readEnvOrError = (env) => {
114
+ let envValue = (0, exports.readEnv)(env);
115
+ if (envValue === undefined) {
116
+ throw new Error(`Environment variable ${env} is not set`);
117
+ }
118
+ return envValue;
119
+ };
120
+ exports.readEnvOrError = readEnvOrError;
121
+ const requireValue = (value, description) => {
122
+ if (value === undefined) {
123
+ throw new Error(`Missing required value: ${description}`);
124
+ }
125
+ return value;
126
+ };
127
+ exports.requireValue = requireValue;
128
+ //# sourceMappingURL=server.js.map
package/server.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;;AAmCtF,sCAwEC;AAKD,kCAMC;AAKD,wCAMC;AA9HD,oEAAoE;AACpE,iEAI4C;AAE5C,+DAAuC;AACvC,8CAAuC;AACvC,6EAAgD;AAOzC,MAAM,YAAY,GAAG,GAAG,EAAE,CAC/B,IAAI,kBAAS,CACX;IACE,IAAI,EAAE,qBAAqB;IAC3B,OAAO,EAAE,OAAO;CACjB,EACD,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAC7C,CAAC;AAPS,QAAA,YAAY,gBAOrB;AAEJ,yBAAyB;AACZ,QAAA,MAAM,GAAG,IAAA,oBAAY,GAAE,CAAC;AAErC;;;GAGG;AACH,SAAgB,aAAa,CAAC,MAI7B;IACC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,YAAY,kBAAS,CAAC,CAAC,CAAC,MAAM,CAAC,M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CAAC,2BAA2B,WAAW,EAAE,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AALW,QAAA,YAAY,gBAKvB"}
package/server.mjs ADDED
@@ -0,0 +1,115 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { CallToolRequestSchema, ListToolsRequestSchema, SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
4
+ import Believe from '@cjavdev/believe';
5
+ import { codeTool } from "./code-tool.mjs";
6
+ import docsSearchTool from "./docs-search-tool.mjs";
7
+ export const newMcpServer = () => new McpServer({
8
+ name: 'cjavdev_believe_api',
9
+ version: '0.5.1',
10
+ }, { capabilities: { tools: {}, logging: {} } });
11
+ // Create server instance
12
+ export const server = newMcpServer();
13
+ /**
14
+ * Initializes the provided MCP Server with the given tools and handlers.
15
+ * If not provided, the default client, tools and handlers will be used.
16
+ */
17
+ export function initMcpServer(params) {
18
+ const server = params.server instanceof McpServer ? params.server.server : params.server;
19
+ const logAtLevel = (level) => (message, ...rest) => {
20
+ void server.sendLoggingMessage({
21
+ level,
22
+ data: { message, rest },
23
+ });
24
+ };
25
+ const logger = {
26
+ debug: logAtLevel('debug'),
27
+ info: logAtLevel('info'),
28
+ warn: logAtLevel('warning'),
29
+ error: logAtLevel('error'),
30
+ };
31
+ let client = new Believe({
32
+ logger,
33
+ ...params.clientOptions,
34
+ defaultHeaders: {
35
+ ...params.clientOptions?.defaultHeaders,
36
+ 'X-Stainless-MCP': 'true',
37
+ },
38
+ });
39
+ const providedTools = selectTools(params.mcpOptions);
40
+ const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
41
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
42
+ return {
43
+ tools: providedTools.map((mcpTool) => mcpTool.tool),
44
+ };
45
+ });
46
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
47
+ const { name, arguments: args } = request.params;
48
+ const mcpTool = toolMap[name];
49
+ if (!mcpTool) {
50
+ throw new Error(`Unknown tool: ${name}`);
51
+ }
52
+ return executeHandler(mcpTool.handler, client, args);
53
+ });
54
+ server.setRequestHandler(SetLevelRequestSchema, async (request) => {
55
+ const { level } = request.params;
56
+ switch (level) {
57
+ case 'debug':
58
+ client = client.withOptions({ logLevel: 'debug' });
59
+ break;
60
+ case 'info':
61
+ client = client.withOptions({ logLevel: 'info' });
62
+ break;
63
+ case 'notice':
64
+ case 'warning':
65
+ client = client.withOptions({ logLevel: 'warn' });
66
+ break;
67
+ case 'error':
68
+ client = client.withOptions({ logLevel: 'error' });
69
+ break;
70
+ default:
71
+ client = client.withOptions({ logLevel: 'off' });
72
+ break;
73
+ }
74
+ return {};
75
+ });
76
+ }
77
+ /**
78
+ * Selects the tools to include in the MCP Server based on the provided options.
79
+ */
80
+ export function selectTools(options) {
81
+ const includedTools = [codeTool()];
82
+ if (options?.includeDocsTools ?? true) {
83
+ includedTools.push(docsSearchTool);
84
+ }
85
+ return includedTools;
86
+ }
87
+ /**
88
+ * Runs the provided handler with the given client and arguments.
89
+ */
90
+ export async function executeHandler(handler, client, args) {
91
+ return await handler(client, args || {});
92
+ }
93
+ export const readEnv = (env) => {
94
+ if (typeof globalThis.process !== 'undefined') {
95
+ return globalThis.process.env?.[env]?.trim();
96
+ }
97
+ else if (typeof globalThis.Deno !== 'undefined') {
98
+ return globalThis.Deno.env?.get?.(env)?.trim();
99
+ }
100
+ return;
101
+ };
102
+ export const readEnvOrError = (env) => {
103
+ let envValue = readEnv(env);
104
+ if (envValue === undefined) {
105
+ throw new Error(`Environment variable ${env} is not set`);
106
+ }
107
+ return envValue;
108
+ };
109
+ export const requireValue = (value, description) => {
110
+ if (value === undefined) {
111
+ throw new Error(`Missing required value: ${description}`);
112
+ }
113
+ return value;
114
+ };
115
+ //# sourceMappingURL=server.mjs.map
package/server.mjs.map ADDED
@@ -0,0 +1 @@
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C,EAAE,IAAI,EAAE,CAAC;IACxD,CAAC;SAAM,IAAI,OAAQ,UAAkB,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QAC3D,OAAQ,UAAkB,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;IAC1D,CAAC;IACD,OAAO;AACT,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAW,EAAU,EAAE;IACpD,IAAI,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,aAAa,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAI,KAAoB,EAAE,WAAmB,EAAK,EAAE;IAC9E,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,2BAA2B,WAAW,EAAE,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
@@ -0,0 +1,16 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { ClientOptions } from '@cjavdev/believe';
4
+
5
+ export type WorkerInput = {
6
+ project_name: string;
7
+ code: string;
8
+ client_opts: ClientOptions;
9
+ intent?: string | undefined;
10
+ };
11
+ export type WorkerOutput = {
12
+ is_error: boolean;
13
+ result: unknown | null;
14
+ log_lines: string[];
15
+ err_lines: string[];
16
+ };
@@ -0,0 +1,112 @@
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 { Believe } from '@cjavdev/believe';
8
+
9
+ const prompt = `Runs JavaScript code to interact with the Believe API.
10
+
11
+ You are a skilled programmer writing code to interface with the service.
12
+ Define an async function named "run" that takes a single parameter of an initialized SDK client and it will be run.
13
+ For example:
14
+
15
+ \`\`\`
16
+ async function run(client) {
17
+ const page = await client.characters.list();
18
+ const character = page.data[0]
19
+
20
+ console.log(character.id);
21
+ }
22
+ \`\`\`
23
+
24
+ You will be returned anything that your function returns, plus the results of any console.log statements.
25
+ Do not add try-catch blocks for single API calls. The tool will handle errors for you.
26
+ Do not add comments unless necessary for generating better code.
27
+ Code will run in a container, and cannot interact with the network outside of the given SDK client.
28
+ Variables will not persist between calls, so make sure to return or log any data you might need later.`;
29
+
30
+ /**
31
+ * A tool that runs code against a copy of the SDK.
32
+ *
33
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
34
+ * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
35
+ * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
36
+ *
37
+ * @param endpoints - The endpoints to include in the list.
38
+ */
39
+ export function codeTool(): McpTool {
40
+ const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
41
+ const tool: Tool = {
42
+ name: 'execute',
43
+ description: prompt,
44
+ inputSchema: {
45
+ type: 'object',
46
+ properties: {
47
+ code: {
48
+ type: 'string',
49
+ description: 'Code to execute.',
50
+ },
51
+ intent: {
52
+ type: 'string',
53
+ description: 'Task you are trying to perform. Used for improving the service.',
54
+ },
55
+ },
56
+ required: ['code'],
57
+ },
58
+ };
59
+ const handler = async (client: Believe, args: any): Promise<ToolCallResult> => {
60
+ const code = args.code as string;
61
+ const intent = args.intent as string | undefined;
62
+
63
+ // this is not required, but passing a Stainless API key for the matching project_name
64
+ // will allow you to run code-mode queries against non-published versions of your SDK.
65
+ const stainlessAPIKey = readEnv('STAINLESS_API_KEY');
66
+ const codeModeEndpoint =
67
+ readEnv('CODE_MODE_ENDPOINT_URL') ?? 'https://api.stainless.com/api/ai/code-tool';
68
+
69
+ const res = await fetch(codeModeEndpoint, {
70
+ method: 'POST',
71
+ headers: {
72
+ ...(stainlessAPIKey && { Authorization: stainlessAPIKey }),
73
+ 'Content-Type': 'application/json',
74
+ client_envs: JSON.stringify({
75
+ BELIEVE_API_KEY: requireValue(
76
+ readEnv('BELIEVE_API_KEY') ?? client.apiKey,
77
+ 'set BELIEVE_API_KEY environment variable or provide apiKey client option',
78
+ ),
79
+ BELIEVE_BASE_URL: readEnv('BELIEVE_BASE_URL') ?? client.baseURL ?? undefined,
80
+ }),
81
+ },
82
+ body: JSON.stringify({
83
+ project_name: 'believe',
84
+ code,
85
+ intent,
86
+ client_opts: {},
87
+ } satisfies WorkerInput),
88
+ });
89
+
90
+ if (!res.ok) {
91
+ throw new Error(
92
+ `${res.status}: ${
93
+ res.statusText
94
+ } error when trying to contact Code Tool server. Details: ${await res.text()}`,
95
+ );
96
+ }
97
+
98
+ const { is_error, result, log_lines, err_lines } = (await res.json()) as WorkerOutput;
99
+ const hasLogs = log_lines.length > 0 || err_lines.length > 0;
100
+ const output = {
101
+ result,
102
+ ...(log_lines.length > 0 && { log_lines }),
103
+ ...(err_lines.length > 0 && { err_lines }),
104
+ };
105
+ if (is_error) {
106
+ return asErrorResult(typeof result === 'string' && !hasLogs ? result : JSON.stringify(output, null, 2));
107
+ }
108
+ return asTextContentResult(output);
109
+ };
110
+
111
+ return { metadata, tool, handler };
112
+ }