dodopayments-mcp 2.4.2 → 2.4.4
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 +37 -1
- package/code-tool-worker.d.mts.map +1 -1
- package/code-tool-worker.d.ts.map +1 -1
- package/code-tool-worker.js +190 -7
- package/code-tool-worker.js.map +1 -1
- package/code-tool-worker.mjs +190 -7
- package/code-tool-worker.mjs.map +1 -1
- package/code-tool.d.mts +1 -1
- package/code-tool.d.mts.map +1 -1
- package/code-tool.d.ts +1 -1
- package/code-tool.d.ts.map +1 -1
- package/code-tool.js +11 -13
- package/code-tool.js.map +1 -1
- package/code-tool.mjs +11 -13
- package/code-tool.mjs.map +1 -1
- package/dynamic-tools.d.mts +1 -1
- package/dynamic-tools.d.ts +1 -1
- package/dynamic-tools.js +1 -1
- package/dynamic-tools.mjs +1 -1
- package/options.d.mts.map +1 -1
- package/options.d.ts.map +1 -1
- package/options.js +6 -3
- package/options.js.map +1 -1
- package/options.mjs +6 -3
- package/options.mjs.map +1 -1
- package/package.json +4 -2
- package/server.js +1 -1
- package/server.mjs +1 -1
- package/src/code-tool-worker.ts +235 -7
- package/src/code-tool.ts +14 -13
- package/src/dynamic-tools.ts +1 -1
- package/src/options.ts +10 -3
- package/src/server.ts +1 -1
package/src/code-tool-worker.ts
CHANGED
|
@@ -1,11 +1,243 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
3
|
import util from 'node:util';
|
|
4
|
+
|
|
5
|
+
import Fuse from 'fuse.js';
|
|
6
|
+
import ts from 'typescript';
|
|
7
|
+
|
|
4
8
|
import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
|
|
5
9
|
import { DodoPayments } from 'dodopayments';
|
|
6
10
|
|
|
11
|
+
function getRunFunctionNode(
|
|
12
|
+
code: string,
|
|
13
|
+
): ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction | null {
|
|
14
|
+
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
|
|
15
|
+
|
|
16
|
+
for (const statement of sourceFile.statements) {
|
|
17
|
+
// Check for top-level function declarations
|
|
18
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
19
|
+
if (statement.name?.text === 'run') {
|
|
20
|
+
return statement;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Check for variable declarations: const run = () => {} or const run = function() {}
|
|
25
|
+
if (ts.isVariableStatement(statement)) {
|
|
26
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
27
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === 'run') {
|
|
28
|
+
// Check if it's initialized with a function
|
|
29
|
+
if (
|
|
30
|
+
declaration.initializer &&
|
|
31
|
+
(ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
|
|
32
|
+
) {
|
|
33
|
+
return declaration.initializer;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const fuse = new Fuse(
|
|
44
|
+
[
|
|
45
|
+
'client.checkoutSessions.create',
|
|
46
|
+
'client.checkoutSessions.retrieve',
|
|
47
|
+
'client.payments.create',
|
|
48
|
+
'client.payments.list',
|
|
49
|
+
'client.payments.retrieve',
|
|
50
|
+
'client.payments.retrieveLineItems',
|
|
51
|
+
'client.subscriptions.changePlan',
|
|
52
|
+
'client.subscriptions.charge',
|
|
53
|
+
'client.subscriptions.create',
|
|
54
|
+
'client.subscriptions.list',
|
|
55
|
+
'client.subscriptions.retrieve',
|
|
56
|
+
'client.subscriptions.retrieveUsageHistory',
|
|
57
|
+
'client.subscriptions.update',
|
|
58
|
+
'client.invoices.payments.retrieve',
|
|
59
|
+
'client.invoices.payments.retrieveRefund',
|
|
60
|
+
'client.licenses.activate',
|
|
61
|
+
'client.licenses.deactivate',
|
|
62
|
+
'client.licenses.validate',
|
|
63
|
+
'client.licenseKeys.list',
|
|
64
|
+
'client.licenseKeys.retrieve',
|
|
65
|
+
'client.licenseKeys.update',
|
|
66
|
+
'client.licenseKeyInstances.list',
|
|
67
|
+
'client.licenseKeyInstances.retrieve',
|
|
68
|
+
'client.licenseKeyInstances.update',
|
|
69
|
+
'client.customers.create',
|
|
70
|
+
'client.customers.list',
|
|
71
|
+
'client.customers.retrieve',
|
|
72
|
+
'client.customers.update',
|
|
73
|
+
'client.customers.customerPortal.create',
|
|
74
|
+
'client.customers.wallets.list',
|
|
75
|
+
'client.customers.wallets.ledgerEntries.create',
|
|
76
|
+
'client.customers.wallets.ledgerEntries.list',
|
|
77
|
+
'client.refunds.create',
|
|
78
|
+
'client.refunds.list',
|
|
79
|
+
'client.refunds.retrieve',
|
|
80
|
+
'client.disputes.list',
|
|
81
|
+
'client.disputes.retrieve',
|
|
82
|
+
'client.payouts.list',
|
|
83
|
+
'client.products.archive',
|
|
84
|
+
'client.products.create',
|
|
85
|
+
'client.products.list',
|
|
86
|
+
'client.products.retrieve',
|
|
87
|
+
'client.products.unarchive',
|
|
88
|
+
'client.products.update',
|
|
89
|
+
'client.products.updateFiles',
|
|
90
|
+
'client.products.images.update',
|
|
91
|
+
'client.misc.listSupportedCountries',
|
|
92
|
+
'client.discounts.create',
|
|
93
|
+
'client.discounts.delete',
|
|
94
|
+
'client.discounts.list',
|
|
95
|
+
'client.discounts.retrieve',
|
|
96
|
+
'client.discounts.update',
|
|
97
|
+
'client.addons.create',
|
|
98
|
+
'client.addons.list',
|
|
99
|
+
'client.addons.retrieve',
|
|
100
|
+
'client.addons.update',
|
|
101
|
+
'client.addons.updateImages',
|
|
102
|
+
'client.brands.create',
|
|
103
|
+
'client.brands.list',
|
|
104
|
+
'client.brands.retrieve',
|
|
105
|
+
'client.brands.update',
|
|
106
|
+
'client.brands.updateImages',
|
|
107
|
+
'client.webhooks.create',
|
|
108
|
+
'client.webhooks.delete',
|
|
109
|
+
'client.webhooks.list',
|
|
110
|
+
'client.webhooks.retrieve',
|
|
111
|
+
'client.webhooks.retrieveSecret',
|
|
112
|
+
'client.webhooks.unsafeUnwrap',
|
|
113
|
+
'client.webhooks.unwrap',
|
|
114
|
+
'client.webhooks.update',
|
|
115
|
+
'client.webhooks.headers.retrieve',
|
|
116
|
+
'client.webhooks.headers.update',
|
|
117
|
+
'client.usageEvents.ingest',
|
|
118
|
+
'client.usageEvents.list',
|
|
119
|
+
'client.usageEvents.retrieve',
|
|
120
|
+
'client.meters.archive',
|
|
121
|
+
'client.meters.create',
|
|
122
|
+
'client.meters.list',
|
|
123
|
+
'client.meters.retrieve',
|
|
124
|
+
'client.meters.unarchive',
|
|
125
|
+
],
|
|
126
|
+
{ threshold: 1, shouldSort: true },
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
|
|
130
|
+
return fuse
|
|
131
|
+
.search(fullyQualifiedMethodName)
|
|
132
|
+
.map(({ item }) => item)
|
|
133
|
+
.slice(0, 5);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const proxyToObj = new WeakMap<any, any>();
|
|
137
|
+
const objToProxy = new WeakMap<any, any>();
|
|
138
|
+
|
|
139
|
+
type ClientProxyConfig = {
|
|
140
|
+
path: string[];
|
|
141
|
+
isBelievedBad?: boolean;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
|
|
145
|
+
let proxy: T = objToProxy.get(obj);
|
|
146
|
+
|
|
147
|
+
if (!proxy) {
|
|
148
|
+
proxy = new Proxy(obj, {
|
|
149
|
+
get(target, prop, receiver) {
|
|
150
|
+
const propPath = [...path, String(prop)];
|
|
151
|
+
const value = Reflect.get(target, prop, receiver);
|
|
152
|
+
|
|
153
|
+
if (isBelievedBad || (!(prop in target) && value === undefined)) {
|
|
154
|
+
// If we're accessing a path that doesn't exist, it will probably eventually error.
|
|
155
|
+
// Let's proxy it and mark it bad so that we can control the error message.
|
|
156
|
+
// We proxy an empty class so that an invocation or construction attempt is possible.
|
|
157
|
+
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
|
|
161
|
+
return makeSdkProxy(value, { path: propPath, isBelievedBad });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return value;
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
apply(target, thisArg, args) {
|
|
168
|
+
if (isBelievedBad || typeof target !== 'function') {
|
|
169
|
+
const fullyQualifiedMethodName = path.join('.');
|
|
170
|
+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
171
|
+
throw new Error(
|
|
172
|
+
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
construct(target, args, newTarget) {
|
|
180
|
+
if (isBelievedBad || typeof target !== 'function') {
|
|
181
|
+
const fullyQualifiedMethodName = path.join('.');
|
|
182
|
+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
183
|
+
throw new Error(
|
|
184
|
+
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return Reflect.construct(target, args, newTarget);
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
objToProxy.set(obj, proxy);
|
|
193
|
+
proxyToObj.set(proxy, obj);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return proxy;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function parseError(code: string, error: unknown): string | undefined {
|
|
200
|
+
if (!(error instanceof Error)) return;
|
|
201
|
+
const message = error.name ? `${error.name}: ${error.message}` : error.message;
|
|
202
|
+
try {
|
|
203
|
+
// Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
|
|
204
|
+
const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
|
|
205
|
+
// -1 for the zero-based indexing
|
|
206
|
+
const line =
|
|
207
|
+
lineNumber &&
|
|
208
|
+
code
|
|
209
|
+
.split('\n')
|
|
210
|
+
.at(parseInt(lineNumber, 10) - 1)
|
|
211
|
+
?.trim();
|
|
212
|
+
return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
|
|
213
|
+
} catch {
|
|
214
|
+
return message;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
7
218
|
const fetch = async (req: Request): Promise<Response> => {
|
|
8
219
|
const { opts, code } = (await req.json()) as WorkerInput;
|
|
220
|
+
if (code == null) {
|
|
221
|
+
return Response.json(
|
|
222
|
+
{
|
|
223
|
+
message:
|
|
224
|
+
'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
|
|
225
|
+
} satisfies WorkerError,
|
|
226
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const runFunctionNode = getRunFunctionNode(code);
|
|
231
|
+
if (!runFunctionNode) {
|
|
232
|
+
return Response.json(
|
|
233
|
+
{
|
|
234
|
+
message:
|
|
235
|
+
'The code is missing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
|
|
236
|
+
} satisfies WorkerError,
|
|
237
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
9
241
|
const client = new DodoPayments({
|
|
10
242
|
...opts,
|
|
11
243
|
});
|
|
@@ -22,21 +254,17 @@ const fetch = async (req: Request): Promise<Response> => {
|
|
|
22
254
|
};
|
|
23
255
|
try {
|
|
24
256
|
let run_ = async (client: any) => {};
|
|
25
|
-
eval(
|
|
26
|
-
|
|
27
|
-
run_ = run;
|
|
28
|
-
`);
|
|
29
|
-
const result = await run_(client);
|
|
257
|
+
eval(`${code}\nrun_ = run;`);
|
|
258
|
+
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
|
|
30
259
|
return Response.json({
|
|
31
260
|
result,
|
|
32
261
|
logLines,
|
|
33
262
|
errLines,
|
|
34
263
|
} satisfies WorkerSuccess);
|
|
35
264
|
} catch (e) {
|
|
36
|
-
const message = e instanceof Error ? e.message : undefined;
|
|
37
265
|
return Response.json(
|
|
38
266
|
{
|
|
39
|
-
message,
|
|
267
|
+
message: parseError(code, e),
|
|
40
268
|
} satisfies WorkerError,
|
|
41
269
|
{ status: 400, statusText: 'Code execution error' },
|
|
42
270
|
);
|
package/src/code-tool.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { dirname } from 'node:path';
|
|
4
4
|
import { pathToFileURL } from 'node:url';
|
|
5
5
|
import DodoPayments, { ClientOptions } from 'dodopayments';
|
|
6
|
-
import {
|
|
6
|
+
import { ContentBlock, Endpoint, Metadata, ToolCallResult } from './tools/types';
|
|
7
7
|
|
|
8
8
|
import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
9
9
|
|
|
@@ -12,7 +12,7 @@ import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
|
|
|
12
12
|
/**
|
|
13
13
|
* A tool that runs code against a copy of the SDK.
|
|
14
14
|
*
|
|
15
|
-
* Instead of exposing every endpoint as
|
|
15
|
+
* Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
|
|
16
16
|
* we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
|
|
17
17
|
* a generic endpoint that can be used to invoke any endpoint with the provided arguments.
|
|
18
18
|
*
|
|
@@ -23,7 +23,7 @@ export async function codeTool(): Promise<Endpoint> {
|
|
|
23
23
|
const tool: Tool = {
|
|
24
24
|
name: 'execute',
|
|
25
25
|
description:
|
|
26
|
-
'Runs
|
|
26
|
+
'Runs TypeScript code to interact with the API.\n\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client named "client", and it will be run.\nWrite code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```\n\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
|
|
27
27
|
inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
|
|
28
28
|
};
|
|
29
29
|
|
|
@@ -31,7 +31,7 @@ export async function codeTool(): Promise<Endpoint> {
|
|
|
31
31
|
const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
|
|
32
32
|
const { workerPath } = await import('./code-tool-paths.cjs');
|
|
33
33
|
|
|
34
|
-
const handler = async (client: DodoPayments, args: unknown) => {
|
|
34
|
+
const handler = async (client: DodoPayments, args: unknown): Promise<ToolCallResult> => {
|
|
35
35
|
const baseURLHostname = new URL(client.baseURL).hostname;
|
|
36
36
|
const { code } = args as { code: string };
|
|
37
37
|
|
|
@@ -98,7 +98,7 @@ export async function codeTool(): Promise<Endpoint> {
|
|
|
98
98
|
} satisfies WorkerInput);
|
|
99
99
|
|
|
100
100
|
req.write(body, (err) => {
|
|
101
|
-
if (err
|
|
101
|
+
if (err != null) {
|
|
102
102
|
reject(err);
|
|
103
103
|
}
|
|
104
104
|
});
|
|
@@ -109,12 +109,12 @@ export async function codeTool(): Promise<Endpoint> {
|
|
|
109
109
|
if (resp.status === 200) {
|
|
110
110
|
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
|
|
111
111
|
const returnOutput: ContentBlock | null =
|
|
112
|
-
result
|
|
113
|
-
|
|
114
|
-
: {
|
|
112
|
+
result == null ? null : (
|
|
113
|
+
{
|
|
115
114
|
type: 'text',
|
|
116
|
-
text: typeof result === 'string' ?
|
|
117
|
-
}
|
|
115
|
+
text: typeof result === 'string' ? result : JSON.stringify(result),
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
118
|
const logOutput: ContentBlock | null =
|
|
119
119
|
logLines.length === 0 ?
|
|
120
120
|
null
|
|
@@ -134,10 +134,11 @@ export async function codeTool(): Promise<Endpoint> {
|
|
|
134
134
|
};
|
|
135
135
|
} else {
|
|
136
136
|
const { message } = (await resp.json()) as WorkerError;
|
|
137
|
-
|
|
137
|
+
return {
|
|
138
|
+
content: message == null ? [] : [{ type: 'text', text: message }],
|
|
139
|
+
isError: true,
|
|
140
|
+
};
|
|
138
141
|
}
|
|
139
|
-
} catch (e) {
|
|
140
|
-
throw e;
|
|
141
142
|
} finally {
|
|
142
143
|
worker.terminate();
|
|
143
144
|
}
|
package/src/dynamic-tools.ts
CHANGED
|
@@ -14,7 +14,7 @@ function zodToInputSchema(schema: z.ZodSchema) {
|
|
|
14
14
|
/**
|
|
15
15
|
* A list of tools that expose all the endpoints in the API dynamically.
|
|
16
16
|
*
|
|
17
|
-
* Instead of exposing every endpoint as
|
|
17
|
+
* Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
|
|
18
18
|
* we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
|
|
19
19
|
* a generic endpoint that can be used to invoke any endpoint with the provided arguments.
|
|
20
20
|
*
|
package/src/options.ts
CHANGED
|
@@ -284,8 +284,10 @@ const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
|
|
|
284
284
|
);
|
|
285
285
|
|
|
286
286
|
const QueryOptions = z.object({
|
|
287
|
-
tools: coerceArray(z.enum(['dynamic', 'all', 'docs'])).describe('
|
|
288
|
-
no_tools: coerceArray(z.enum(['dynamic', 'all', 'docs'])).describe(
|
|
287
|
+
tools: coerceArray(z.enum(['dynamic', 'all', 'code', 'docs'])).describe('Specify which MCP tools to use'),
|
|
288
|
+
no_tools: coerceArray(z.enum(['dynamic', 'all', 'code', 'docs'])).describe(
|
|
289
|
+
'Specify which MCP tools to not use.',
|
|
290
|
+
),
|
|
289
291
|
tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
|
|
290
292
|
resource: coerceArray(z.string()).describe('Include tools matching the specified resources'),
|
|
291
293
|
operation: coerceArray(z.enum(['read', 'write'])).describe(
|
|
@@ -385,11 +387,16 @@ export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): M
|
|
|
385
387
|
: queryOptions.tools?.includes('docs') ? true
|
|
386
388
|
: defaultOptions.includeDocsTools;
|
|
387
389
|
|
|
390
|
+
let codeTools: boolean | undefined =
|
|
391
|
+
queryOptions.no_tools && queryOptions.no_tools?.includes('code') ? false
|
|
392
|
+
: queryOptions.tools?.includes('code') && defaultOptions.includeCodeTools ? true
|
|
393
|
+
: defaultOptions.includeCodeTools;
|
|
394
|
+
|
|
388
395
|
return {
|
|
389
396
|
client: queryOptions.client ?? defaultOptions.client,
|
|
390
397
|
includeDynamicTools: dynamicTools,
|
|
391
398
|
includeAllTools: allTools,
|
|
392
|
-
includeCodeTools:
|
|
399
|
+
includeCodeTools: codeTools,
|
|
393
400
|
includeDocsTools: docsTools,
|
|
394
401
|
filters,
|
|
395
402
|
capabilities: clientCapabilities,
|