@vllnt/convex-helpers 0.1.0-canary.1e0f54a
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 +21 -0
- package/README.md +125 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/auth.d.ts +11 -0
- package/dist/mcp/auth.d.ts.map +1 -0
- package/dist/mcp/auth.js +36 -0
- package/dist/mcp/auth.js.map +1 -0
- package/dist/mcp/index.d.ts +7 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +5 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/pagination/context.d.ts +9 -0
- package/dist/mcp/pagination/context.d.ts.map +1 -0
- package/dist/mcp/pagination/context.js +21 -0
- package/dist/mcp/pagination/context.js.map +1 -0
- package/dist/mcp/pagination/cursor.d.ts +7 -0
- package/dist/mcp/pagination/cursor.d.ts.map +1 -0
- package/dist/mcp/pagination/cursor.js +60 -0
- package/dist/mcp/pagination/cursor.js.map +1 -0
- package/dist/mcp/pagination/handlers.d.ts +12 -0
- package/dist/mcp/pagination/handlers.d.ts.map +1 -0
- package/dist/mcp/pagination/handlers.js +91 -0
- package/dist/mcp/pagination/handlers.js.map +1 -0
- package/dist/mcp/pagination/types.d.ts +26 -0
- package/dist/mcp/pagination/types.d.ts.map +1 -0
- package/dist/mcp/pagination/types.js +2 -0
- package/dist/mcp/pagination/types.js.map +1 -0
- package/dist/mcp/resources/helpers.d.ts +9 -0
- package/dist/mcp/resources/helpers.d.ts.map +1 -0
- package/dist/mcp/resources/helpers.js +8 -0
- package/dist/mcp/resources/helpers.js.map +1 -0
- package/dist/mcp/resources/register.d.ts +14 -0
- package/dist/mcp/resources/register.d.ts.map +1 -0
- package/dist/mcp/resources/register.js +40 -0
- package/dist/mcp/resources/register.js.map +1 -0
- package/dist/mcp/resources/types.d.ts +7 -0
- package/dist/mcp/resources/types.d.ts.map +1 -0
- package/dist/mcp/resources/types.js +2 -0
- package/dist/mcp/resources/types.js.map +1 -0
- package/dist/mcp/server.d.ts +3 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +139 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools/helpers.d.ts +16 -0
- package/dist/mcp/tools/helpers.d.ts.map +1 -0
- package/dist/mcp/tools/helpers.js +34 -0
- package/dist/mcp/tools/helpers.js.map +1 -0
- package/dist/mcp/tools/register.d.ts +23 -0
- package/dist/mcp/tools/register.d.ts.map +1 -0
- package/dist/mcp/tools/register.js +195 -0
- package/dist/mcp/tools/register.js.map +1 -0
- package/dist/mcp/tools/types.d.ts +71 -0
- package/dist/mcp/tools/types.d.ts.map +1 -0
- package/dist/mcp/tools/types.js +2 -0
- package/dist/mcp/tools/types.js.map +1 -0
- package/dist/mcp/types.d.ts +50 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/mcp/validators.d.ts +8 -0
- package/dist/mcp/validators.d.ts.map +1 -0
- package/dist/mcp/validators.js +135 -0
- package/dist/mcp/validators.js.map +1 -0
- package/package.json +95 -0
- package/src/index.test.ts +164 -0
- package/src/index.ts +62 -0
- package/src/mcp/auth.ts +53 -0
- package/src/mcp/index.ts +23 -0
- package/src/mcp/pagination/context.ts +27 -0
- package/src/mcp/pagination/cursor.ts +99 -0
- package/src/mcp/pagination/handlers.ts +172 -0
- package/src/mcp/pagination/types.ts +27 -0
- package/src/mcp/resources/helpers.ts +19 -0
- package/src/mcp/resources/register.ts +63 -0
- package/src/mcp/resources/types.ts +7 -0
- package/src/mcp/server.ts +209 -0
- package/src/mcp/tools/helpers.ts +47 -0
- package/src/mcp/tools/register.ts +256 -0
- package/src/mcp/tools/types.ts +74 -0
- package/src/mcp/types.ts +65 -0
- package/src/mcp/validators.ts +175 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bntvllnt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<!-- Badges -->
|
|
2
|
+
[](https://www.npmjs.com/package/@vllnt/convex-helpers)
|
|
3
|
+
[](https://github.com/vllnt/convex-helpers/actions/workflows/ci.yml)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
# @vllnt/convex-helpers
|
|
7
|
+
|
|
8
|
+
Typed host-side helpers for Convex backends — builders, errors, validators, relationships,
|
|
9
|
+
pagination, HTTP, env, and observability.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { asyncMap, pruneNull, nullThrows } from "@vllnt/convex-helpers";
|
|
13
|
+
|
|
14
|
+
const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
15
|
+
const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
A helpers library (not a sandboxed Convex component): pure functions and host-`ctx` glue that run with
|
|
19
|
+
the host's `ctx` — no own tables, no `app.use()` mounting. Stateful concerns (rate limiting,
|
|
20
|
+
idempotency, flags) belong in `@convex-dev/*` or `@vllnt/convex-*` components.
|
|
21
|
+
|
|
22
|
+
See [ROADMAP.md](./ROADMAP.md) for the planned module surface.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- `asyncMap` — parallel async map that preserves order
|
|
27
|
+
- `pruneNull` — filter null/undefined from arrays
|
|
28
|
+
- `nullThrows` — non-null assertion with typed `NullDocumentError`
|
|
29
|
+
- `./mcp` — expose Convex functions as MCP tools (`createMCPServer` + `query`/`mutation`/`action`/`resource`), default-deny auth, optional cursor pagination
|
|
30
|
+
- [planned] `./builders` — `customQuery`/`customMutation`/`customAction`/`customCtx`
|
|
31
|
+
- [planned] `./errors` — typed `AppError` + HTTP-status map
|
|
32
|
+
- [planned] `./auth` — provider-agnostic `requireIdentity`/`getCurrentSubject`
|
|
33
|
+
- [planned] `./env` — `defineEnv(zodSchema)` cold-start validation
|
|
34
|
+
- [planned] `./tracing` — span emit + `traceparent` propagation via `@vllnt/logger`
|
|
35
|
+
- [planned] `./testing` — fixture factories + `withIdentity`
|
|
36
|
+
- [planned] `./relationships`, `./validators`, `./pagination`, `./rls`, `./triggers`, `./http`
|
|
37
|
+
- [planned] `./react` — optional tree-shakeable front-end hooks layer
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pnpm add @vllnt/convex-helpers
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`convex` is a peer dependency:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pnpm add convex
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { asyncMap, pruneNull, nullThrows } from "@vllnt/convex-helpers";
|
|
55
|
+
|
|
56
|
+
// Parallel fetch with null filtering
|
|
57
|
+
const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
58
|
+
|
|
59
|
+
// Non-null assertion
|
|
60
|
+
const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### MCP tools — `./mcp`
|
|
64
|
+
|
|
65
|
+
Expose Convex functions to LLM agents over the Model Context Protocol. The `./mcp` entry is
|
|
66
|
+
tree-shakeable — backend-only consumers pull zero MCP code. It needs two optional peer deps:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pnpm add @modelcontextprotocol/sdk zod
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { createMCPServer, query, mutation } from "@vllnt/convex-helpers/mcp";
|
|
74
|
+
import { api } from "./_generated/api";
|
|
75
|
+
import { v } from "convex/values";
|
|
76
|
+
|
|
77
|
+
export const mcp = createMCPServer({
|
|
78
|
+
auth: { validate: async (key) => key === process.env.MCP_API_KEY },
|
|
79
|
+
tools: {
|
|
80
|
+
list_projects: query(api.projects.list, { args: v.object({}), description: "List all projects" }),
|
|
81
|
+
create_project: mutation(api.projects.create, {
|
|
82
|
+
args: v.object({ name: v.string() }),
|
|
83
|
+
description: "Create a project",
|
|
84
|
+
}),
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Mount the route handler (e.g. Next.js App Router)
|
|
89
|
+
export const { GET, POST } = mcp.handler();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Migrating from `@vllnt/convex-mcp`? Change the import to `@vllnt/convex-helpers/mcp` — the API is
|
|
93
|
+
identical. See [docs/API.md](./docs/API.md#mcp-tools-mcp) for the full `./mcp` reference.
|
|
94
|
+
|
|
95
|
+
## API Reference
|
|
96
|
+
|
|
97
|
+
See [docs/API.md](./docs/API.md) for full API reference with signatures and examples.
|
|
98
|
+
|
|
99
|
+
## Testing
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pnpm test
|
|
103
|
+
pnpm test:coverage # must reach 100%
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Tests use [vitest](https://vitest.dev) with the node environment. 100% coverage is enforced via
|
|
107
|
+
`vitest.config.mts` thresholds.
|
|
108
|
+
|
|
109
|
+
## Contributing
|
|
110
|
+
|
|
111
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, code style, and PR guidelines.
|
|
112
|
+
|
|
113
|
+
## Author
|
|
114
|
+
|
|
115
|
+
Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X
|
|
116
|
+
@bntvllnt](https://x.com/bntvllnt)
|
|
117
|
+
|
|
118
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
119
|
+
[vllnt.com](https://vllnt.com)
|
|
120
|
+
|
|
121
|
+
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
[MIT](./LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @vllnt/convex-helpers
|
|
3
|
+
* Pure host-side helpers for Convex backends.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Applies an async function to every element of an array, returning results in order.
|
|
7
|
+
*
|
|
8
|
+
* @param list - Input array
|
|
9
|
+
* @param fn - Async mapper function
|
|
10
|
+
* @returns Promise resolving to mapped array in original order
|
|
11
|
+
* @example
|
|
12
|
+
* const docs = await asyncMap(ids, (id) => ctx.db.get(id));
|
|
13
|
+
*/
|
|
14
|
+
export declare function asyncMap<T, TResult>(list: T[], fn: (item: T, index: number) => Promise<TResult>): Promise<TResult[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Filters null and undefined values from an array.
|
|
17
|
+
*
|
|
18
|
+
* @param list - Input array potentially containing null/undefined
|
|
19
|
+
* @returns Array with all null and undefined values removed
|
|
20
|
+
* @example
|
|
21
|
+
* const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
22
|
+
*/
|
|
23
|
+
export declare function pruneNull<T>(list: (null | T | undefined)[]): T[];
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when a required document is null or undefined.
|
|
26
|
+
*/
|
|
27
|
+
export declare class NullDocumentError extends Error {
|
|
28
|
+
constructor(message?: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Asserts a value is non-null and non-undefined, throwing NullDocumentError when absent.
|
|
32
|
+
*
|
|
33
|
+
* @param value - Value to check
|
|
34
|
+
* @param message - Optional custom error message
|
|
35
|
+
* @returns The value, narrowed to exclude null and undefined
|
|
36
|
+
* @throws {NullDocumentError} When value is null or undefined
|
|
37
|
+
* @example
|
|
38
|
+
* const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
39
|
+
*/
|
|
40
|
+
export declare function nullThrows<T>(value: null | T | undefined, message?: string): T;
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,OAAO,EACvC,IAAI,EAAE,CAAC,EAAE,EACT,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,GAC/C,OAAO,CAAC,OAAO,EAAE,CAAC,CAEpB;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAEhE;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,OAAO,SAAiC;CAIrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,KAAK,EAAE,IAAI,GAAG,CAAC,GAAG,SAAS,EAC3B,OAAO,CAAC,EAAE,MAAM,GACf,CAAC,CAKH"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @vllnt/convex-helpers
|
|
3
|
+
* Pure host-side helpers for Convex backends.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Applies an async function to every element of an array, returning results in order.
|
|
7
|
+
*
|
|
8
|
+
* @param list - Input array
|
|
9
|
+
* @param fn - Async mapper function
|
|
10
|
+
* @returns Promise resolving to mapped array in original order
|
|
11
|
+
* @example
|
|
12
|
+
* const docs = await asyncMap(ids, (id) => ctx.db.get(id));
|
|
13
|
+
*/
|
|
14
|
+
export async function asyncMap(list, fn) {
|
|
15
|
+
return Promise.all(list.map(fn));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Filters null and undefined values from an array.
|
|
19
|
+
*
|
|
20
|
+
* @param list - Input array potentially containing null/undefined
|
|
21
|
+
* @returns Array with all null and undefined values removed
|
|
22
|
+
* @example
|
|
23
|
+
* const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
24
|
+
*/
|
|
25
|
+
export function pruneNull(list) {
|
|
26
|
+
return list.filter((item) => item !== null && item !== undefined);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Error thrown when a required document is null or undefined.
|
|
30
|
+
*/
|
|
31
|
+
export class NullDocumentError extends Error {
|
|
32
|
+
constructor(message = "Expected a non-null document") {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "NullDocumentError";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Asserts a value is non-null and non-undefined, throwing NullDocumentError when absent.
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value to check
|
|
41
|
+
* @param message - Optional custom error message
|
|
42
|
+
* @returns The value, narrowed to exclude null and undefined
|
|
43
|
+
* @throws {NullDocumentError} When value is null or undefined
|
|
44
|
+
* @example
|
|
45
|
+
* const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
46
|
+
*/
|
|
47
|
+
export function nullThrows(value, message) {
|
|
48
|
+
if (value === null || value === undefined) {
|
|
49
|
+
throw new NullDocumentError(message);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAS,EACT,EAAgD;IAEhD,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAI,IAA8B;IACzD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAa,EAAE,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,CAAC,CAAC;AAC/E,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,OAAO,GAAG,8BAA8B;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,UAAU,CACxB,KAA2B,EAC3B,OAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,MAAM,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AuthConfig } from "./types.js";
|
|
2
|
+
export declare function extractApiKey(request: Request): string | undefined;
|
|
3
|
+
export declare function validateRequest(request: Request, auth: AuthConfig): Promise<{
|
|
4
|
+
apiKey: string;
|
|
5
|
+
convexToken?: string;
|
|
6
|
+
valid: true;
|
|
7
|
+
} | {
|
|
8
|
+
response: Response;
|
|
9
|
+
valid: false;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/mcp/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CASlE;AAED,wBAAsB,eAAe,CACnC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,UAAU,GACf,OAAO,CACN;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,IAAI,CAAA;CAAE,GACrD;IAAE,QAAQ,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CACvC,CAiCA"}
|
package/dist/mcp/auth.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function extractApiKey(request) {
|
|
2
|
+
const authHeader = request.headers.get("authorization");
|
|
3
|
+
if (!authHeader)
|
|
4
|
+
return undefined;
|
|
5
|
+
if (authHeader.startsWith("Bearer ")) {
|
|
6
|
+
return authHeader.slice(7);
|
|
7
|
+
}
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
export async function validateRequest(request, auth) {
|
|
11
|
+
const apiKey = extractApiKey(request);
|
|
12
|
+
if (!apiKey) {
|
|
13
|
+
return {
|
|
14
|
+
response: new Response(JSON.stringify({
|
|
15
|
+
error: "Missing or malformed API key. Use Authorization: Bearer <key>.",
|
|
16
|
+
}), { headers: { "Content-Type": "application/json" }, status: 401 }),
|
|
17
|
+
valid: false,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const isValid = await auth.validate(apiKey);
|
|
21
|
+
if (!isValid) {
|
|
22
|
+
return {
|
|
23
|
+
response: new Response(JSON.stringify({ error: "Invalid API key." }), {
|
|
24
|
+
headers: { "Content-Type": "application/json" },
|
|
25
|
+
status: 401,
|
|
26
|
+
}),
|
|
27
|
+
valid: false,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
let convexToken;
|
|
31
|
+
if (auth.convexToken) {
|
|
32
|
+
convexToken = await auth.convexToken(apiKey);
|
|
33
|
+
}
|
|
34
|
+
return { apiKey, convexToken, valid: true };
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/mcp/auth.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,aAAa,CAAC,OAAgB;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IACxD,IAAI,CAAC,UAAU;QAAE,OAAO,SAAS,CAAC;IAElC,IAAI,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QACrC,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC7B,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,OAAgB,EAChB,IAAgB;IAKhB,MAAM,MAAM,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IAEtC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;YACL,QAAQ,EAAE,IAAI,QAAQ,CACpB,IAAI,CAAC,SAAS,CAAC;gBACb,KAAK,EACH,gEAAgE;aACnE,CAAC,EACF,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CACjE;YACD,KAAK,EAAE,KAAK;SACb,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,QAAQ,EAAE,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,kBAAkB,EAAE,CAAC,EAAE;gBACpE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;gBAC/C,MAAM,EAAE,GAAG;aACZ,CAAC;YACF,KAAK,EAAE,KAAK;SACb,CAAC;IACJ,CAAC;IAED,IAAI,WAA+B,CAAC;IACpC,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;QACrB,WAAW,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC9C,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { ToolPage, ToolSummary } from "./pagination/types.js";
|
|
2
|
+
export { resource } from "./resources/helpers.js";
|
|
3
|
+
export { createMCPServer } from "./server.js";
|
|
4
|
+
export { action, mutation, query } from "./tools/helpers.js";
|
|
5
|
+
export type { AuthConfig, CallContext, ConvexClient, ConvexMCPServer, ConvexValidator, FunctionType, LifecycleHooks, OnCallResult, PaginationConfig, ResourceDef, ServerConfig, ToolDef, } from "./types.js";
|
|
6
|
+
export { convertValidator, convexArgsToZod, UnsupportedValidatorError, } from "./validators.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC7D,YAAY,EACV,UAAU,EACV,WAAW,EACX,YAAY,EACZ,eAAe,EACf,eAAe,EACf,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,WAAW,EACX,YAAY,EACZ,OAAO,GACR,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,yBAAyB,GAC1B,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { resource } from "./resources/helpers.js";
|
|
2
|
+
export { createMCPServer } from "./server.js";
|
|
3
|
+
export { action, mutation, query } from "./tools/helpers.js";
|
|
4
|
+
export { convertValidator, convexArgsToZod, UnsupportedValidatorError, } from "./validators.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAe7D,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,yBAAyB,GAC1B,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PaginationConfig } from "./types.js";
|
|
2
|
+
/** Create pagination context from config. Validates pageSize >= 1. */
|
|
3
|
+
export declare function createPaginationContext(config: PaginationConfig | undefined): {
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
pageSize: number;
|
|
6
|
+
secret: string;
|
|
7
|
+
twoPhaseDiscovery: boolean;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/mcp/pagination/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,sEAAsE;AACtE,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,gBAAgB,GAAG,SAAS,GAAG;IAC7E,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAkBA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Create pagination context from config. Validates pageSize >= 1. */
|
|
2
|
+
export function createPaginationContext(config) {
|
|
3
|
+
if (!config) {
|
|
4
|
+
return {
|
|
5
|
+
enabled: false,
|
|
6
|
+
pageSize: 0,
|
|
7
|
+
secret: "",
|
|
8
|
+
twoPhaseDiscovery: false,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
if (!Number.isInteger(config.pageSize) || config.pageSize < 1) {
|
|
12
|
+
throw new Error("pagination.pageSize must be a positive integer >= 1");
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
enabled: true,
|
|
16
|
+
pageSize: config.pageSize,
|
|
17
|
+
secret: crypto.randomUUID(),
|
|
18
|
+
twoPhaseDiscovery: config.twoPhaseDiscovery ?? false,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/mcp/pagination/context.ts"],"names":[],"mappings":"AAEA,sEAAsE;AACtE,MAAM,UAAU,uBAAuB,CAAC,MAAoC;IAM1E,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;YACL,OAAO,EAAE,KAAK;YACd,QAAQ,EAAE,CAAC;YACX,MAAM,EAAE,EAAE;YACV,iBAAiB,EAAE,KAAK;SACzB,CAAC;IACJ,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,QAAQ,GAAG,CAAC,EAAE,CAAC;QAC9D,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IACD,OAAO;QACL,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE;QAC3B,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,IAAI,KAAK;KACrD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function encodeCursor(method: string, offset: number, secret: string): Promise<string>;
|
|
2
|
+
export declare function decodeCursor(cursor: string, expectedMethod: string, secret: string): Promise<{
|
|
3
|
+
error: string;
|
|
4
|
+
} | {
|
|
5
|
+
offset: number;
|
|
6
|
+
}>;
|
|
7
|
+
//# sourceMappingURL=cursor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cursor.d.ts","sourceRoot":"","sources":["../../../src/mcp/pagination/cursor.ts"],"names":[],"mappings":"AAgEA,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAMjB;AAED,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAkBjD"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const SEPARATOR = ".";
|
|
2
|
+
function isCursorPayload(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null)
|
|
4
|
+
return false;
|
|
5
|
+
const object = value;
|
|
6
|
+
return (typeof object.v === "number" &&
|
|
7
|
+
typeof object.m === "string" &&
|
|
8
|
+
typeof object.o === "number");
|
|
9
|
+
}
|
|
10
|
+
async function importKey(secret, usages) {
|
|
11
|
+
return crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { hash: "SHA-256", name: "HMAC" }, false, usages);
|
|
12
|
+
}
|
|
13
|
+
async function hmacSign(payload, secret) {
|
|
14
|
+
const key = await importKey(secret, ["sign"]);
|
|
15
|
+
return crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payload));
|
|
16
|
+
}
|
|
17
|
+
async function hmacVerify(payload, signature, secret) {
|
|
18
|
+
let sigBytes;
|
|
19
|
+
try {
|
|
20
|
+
sigBytes = new Uint8Array(Array.from(atob(signature), (c) => c.charCodeAt(0)));
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const key = await importKey(secret, ["verify"]);
|
|
26
|
+
return crypto.subtle.verify("HMAC", key, sigBytes, new TextEncoder().encode(payload));
|
|
27
|
+
}
|
|
28
|
+
function arrayBufferToBase64(buf) {
|
|
29
|
+
return btoa(Array.from(new Uint8Array(buf), (b) => String.fromCharCode(b)).join(""));
|
|
30
|
+
}
|
|
31
|
+
export async function encodeCursor(method, offset, secret) {
|
|
32
|
+
const payload = { m: method, o: offset, v: 1 };
|
|
33
|
+
const json = JSON.stringify(payload);
|
|
34
|
+
const b64 = btoa(json);
|
|
35
|
+
const sig = await hmacSign(b64, secret);
|
|
36
|
+
return `${b64}${SEPARATOR}${arrayBufferToBase64(sig)}`;
|
|
37
|
+
}
|
|
38
|
+
export async function decodeCursor(cursor, expectedMethod, secret) {
|
|
39
|
+
const separatorIndex = cursor.lastIndexOf(SEPARATOR);
|
|
40
|
+
if (separatorIndex === -1)
|
|
41
|
+
return { error: "invalid or expired cursor" };
|
|
42
|
+
const b64 = cursor.slice(0, separatorIndex);
|
|
43
|
+
const sig = cursor.slice(separatorIndex + 1);
|
|
44
|
+
const valid = await hmacVerify(b64, sig, secret);
|
|
45
|
+
if (!valid)
|
|
46
|
+
return { error: "invalid or expired cursor" };
|
|
47
|
+
// Safe to parse without try-catch: HMAC verification above guarantees b64 is
|
|
48
|
+
// a payload we signed, so atob + JSON.parse will not throw.
|
|
49
|
+
const raw = JSON.parse(atob(b64));
|
|
50
|
+
if (!isCursorPayload(raw))
|
|
51
|
+
return { error: "invalid or expired cursor" };
|
|
52
|
+
if (raw.v !== 1)
|
|
53
|
+
return { error: "invalid or expired cursor" };
|
|
54
|
+
if (raw.m !== expectedMethod)
|
|
55
|
+
return { error: "invalid or expired cursor" };
|
|
56
|
+
if (raw.o < 0)
|
|
57
|
+
return { error: "invalid or expired cursor" };
|
|
58
|
+
return { offset: raw.o };
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=cursor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cursor.js","sourceRoot":"","sources":["../../../src/mcp/pagination/cursor.ts"],"names":[],"mappings":"AAMA,MAAM,SAAS,GAAG,GAAG,CAAC;AAEtB,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,MAAM,GAAG,KAAgC,CAAC;IACh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D,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;IAE1D,6EAA6E;IAC7E,4DAA4D;IAC5D,MAAM,GAAG,GAAY,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3C,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;IACzE,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;IAC/D,IAAI,GAAG,CAAC,CAAC,KAAK,cAAc;QAAE,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;IAC5E,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;IAC7D,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
type McpTool = {
|
|
3
|
+
description?: string;
|
|
4
|
+
inputSchema: Record<string, unknown>;
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
type ToolListFn = () => Promise<McpTool[]>;
|
|
8
|
+
export declare function getOriginalToolsList(mcpServer: McpServer): ToolListFn;
|
|
9
|
+
export declare function registerPaginationHandlers(mcpServer: McpServer, getAllTools: ToolListFn, pageSize: number, secret: string): void;
|
|
10
|
+
export declare function registerTwoPhaseHandlers(mcpServer: McpServer, getAllTools: ToolListFn, pageSize: number, secret: string): void;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=handlers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../../src/mcp/pagination/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAWzE,KAAK,OAAO,GAAG;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,UAAU,GAAG,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;AAgB3C,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,SAAS,GAAG,UAAU,CAcrE;AA+BD,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,SAAS,EACpB,WAAW,EAAE,UAAU,EACvB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,IAAI,CAwBN;AAED,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS,EACpB,WAAW,EAAE,UAAU,EACvB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,IAAI,CAyDN"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ErrorCode, ListToolsRequestSchema, McpError, } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { decodeCursor, encodeCursor } from "./cursor.js";
|
|
4
|
+
/* v8 ignore start -- canary: only triggers if SDK removes tools/list handler */
|
|
5
|
+
function assertHandler(handler) {
|
|
6
|
+
if (!handler)
|
|
7
|
+
throw new Error("[convex-mcp] tools/list handler not found — SDK API may have changed.");
|
|
8
|
+
return handler;
|
|
9
|
+
}
|
|
10
|
+
/* v8 ignore stop */
|
|
11
|
+
export function getOriginalToolsList(mcpServer) {
|
|
12
|
+
const handlers = mcpServer.server._requestHandlers;
|
|
13
|
+
const origHandler = assertHandler(handlers.get("tools/list"));
|
|
14
|
+
return async () => {
|
|
15
|
+
const result = await origHandler({ method: "tools/list", params: {} }, { signal: new AbortController().signal });
|
|
16
|
+
return result.tools;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async function paginateTools(tools, method, pageSize, cursor, secret) {
|
|
20
|
+
let offset = 0;
|
|
21
|
+
if (cursor !== "") {
|
|
22
|
+
const decoded = await decodeCursor(cursor, method, secret);
|
|
23
|
+
if ("error" in decoded)
|
|
24
|
+
return { error: decoded.error };
|
|
25
|
+
offset = decoded.offset;
|
|
26
|
+
}
|
|
27
|
+
/* v8 ignore start -- unreachable from public API: HMAC prevents crafting out-of-bounds cursors */
|
|
28
|
+
if (offset >= tools.length && tools.length > 0)
|
|
29
|
+
return { error: "invalid or expired cursor" };
|
|
30
|
+
/* v8 ignore stop */
|
|
31
|
+
const page = tools.slice(offset, offset + pageSize);
|
|
32
|
+
const hasMore = offset + pageSize < tools.length;
|
|
33
|
+
const nextCursor = hasMore
|
|
34
|
+
? await encodeCursor(method, offset + pageSize, secret)
|
|
35
|
+
: undefined;
|
|
36
|
+
return { nextCursor, tools: page };
|
|
37
|
+
}
|
|
38
|
+
export function registerPaginationHandlers(mcpServer, getAllTools, pageSize, secret) {
|
|
39
|
+
mcpServer.server.setRequestHandler(ListToolsRequestSchema, async (request) => {
|
|
40
|
+
const cursor = request.params?.cursor;
|
|
41
|
+
const allTools = await getAllTools();
|
|
42
|
+
if (cursor === undefined) {
|
|
43
|
+
return { tools: allTools };
|
|
44
|
+
}
|
|
45
|
+
const result = await paginateTools(allTools, "tools/list", pageSize, cursor, secret);
|
|
46
|
+
if ("error" in result) {
|
|
47
|
+
throw new McpError(ErrorCode.InvalidParams, result.error);
|
|
48
|
+
}
|
|
49
|
+
return { nextCursor: result.nextCursor, tools: result.tools };
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export function registerTwoPhaseHandlers(mcpServer, getAllTools, pageSize, secret) {
|
|
53
|
+
const listSummarySchema = z.object({
|
|
54
|
+
method: z.literal("tools/list_summary"),
|
|
55
|
+
params: z.object({ cursor: z.string().optional() }).optional(),
|
|
56
|
+
});
|
|
57
|
+
mcpServer.server.setRequestHandler(listSummarySchema, async (request) => {
|
|
58
|
+
const allTools = await getAllTools();
|
|
59
|
+
const summaries = allTools.map((t) => ({
|
|
60
|
+
/* v8 ignore next -- both branches tested; v8 misreports ternary in .map() */
|
|
61
|
+
description: t.description === undefined ? "" : t.description,
|
|
62
|
+
name: t.name,
|
|
63
|
+
}));
|
|
64
|
+
const cursor = request.params?.cursor;
|
|
65
|
+
if (cursor === undefined) {
|
|
66
|
+
return { tools: summaries };
|
|
67
|
+
}
|
|
68
|
+
const result = await paginateTools(summaries, "tools/list_summary", pageSize, cursor, secret);
|
|
69
|
+
if ("error" in result) {
|
|
70
|
+
throw new McpError(ErrorCode.InvalidParams, result.error);
|
|
71
|
+
}
|
|
72
|
+
return { nextCursor: result.nextCursor, tools: result.tools };
|
|
73
|
+
});
|
|
74
|
+
const describeSchema = z.object({
|
|
75
|
+
method: z.literal("tools/describe"),
|
|
76
|
+
params: z.object({ name: z.string().optional() }).optional(),
|
|
77
|
+
});
|
|
78
|
+
mcpServer.server.setRequestHandler(describeSchema, async (request) => {
|
|
79
|
+
const name = request.params?.name;
|
|
80
|
+
if (!name) {
|
|
81
|
+
throw new McpError(ErrorCode.InvalidParams, "Invalid params: 'name' is required");
|
|
82
|
+
}
|
|
83
|
+
const allTools = await getAllTools();
|
|
84
|
+
const tool = allTools.find((t) => t.name === name);
|
|
85
|
+
if (!tool) {
|
|
86
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid params: tool '${name}' not found`);
|
|
87
|
+
}
|
|
88
|
+
return { tool };
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=handlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.js","sourceRoot":"","sources":["../../../src/mcp/pagination/handlers.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,QAAQ,GACT,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE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CAAC,MAAM,CAAC;QAC9B,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC;QACnC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,QAAQ,EAAE;KAC7D,CAAC,CAAC;IAEH,SAAS,CAAC,MAAM,CAAC,iBAAiB,CAAC,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QACnE,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC;QAClC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,QAAQ,CAChB,SAAS,CAAC,aAAa,EACvB,oCAAoC,CACrC,CAAC;QACJ,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,WAAW,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QACnD,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,QAAQ,CAChB,SAAS,CAAC,aAAa,EACvB,yBAAyB,IAAI,aAAa,CAC3C,CAAC;QACJ,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in pagination for `tools/list` and two-phase tool discovery.
|
|
3
|
+
*
|
|
4
|
+
* `pageSize` is required for all pagination features including `twoPhaseDiscovery`.
|
|
5
|
+
* Must be >= 1. When enabled, `tools/list` without a cursor still returns ALL tools
|
|
6
|
+
* (backwards-compatible). Cursor pagination activates only when the client sends a cursor.
|
|
7
|
+
*
|
|
8
|
+
* `twoPhaseDiscovery` enables non-standard custom MCP methods (`tools/list_summary`,
|
|
9
|
+
* `tools/describe`). These are NOT part of the MCP spec — only custom agents that
|
|
10
|
+
* explicitly call these methods will benefit.
|
|
11
|
+
*/
|
|
12
|
+
export type PaginationConfig = {
|
|
13
|
+
/** Number of tools per page when client sends a cursor. Must be >= 1. */
|
|
14
|
+
pageSize: number;
|
|
15
|
+
/** Enable `tools/list_summary` + `tools/describe` custom methods. Default: false. */
|
|
16
|
+
twoPhaseDiscovery?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export type ToolSummary = {
|
|
19
|
+
description: string;
|
|
20
|
+
name: string;
|
|
21
|
+
};
|
|
22
|
+
export type ToolPage<T extends object> = {
|
|
23
|
+
nextCursor?: string;
|
|
24
|
+
tools: T[];
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/mcp/pagination/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB,qFAAqF;IACrF,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI;IACvC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,CAAC,EAAE,CAAC;CACZ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/mcp/pagination/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ConvexValidator } from "../types.js";
|
|
2
|
+
import type { ResourceDef } from "./types.js";
|
|
3
|
+
type ResourceOptions = {
|
|
4
|
+
args?: ConvexValidator;
|
|
5
|
+
description?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function resource(ref: unknown, options?: ResourceOptions): ResourceDef;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/mcp/resources/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,KAAK,eAAe,GAAG;IACrB,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,QAAQ,CACtB,GAAG,EAAE,OAAO,EACZ,OAAO,GAAE,eAAoB,GAC5B,WAAW,CAMb"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/mcp/resources/helpers.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,QAAQ,CACtB,GAAY,EACZ,UAA2B,EAAE;IAE7B,OAAO;QACL,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,GAAG;KACJ,CAAC;AACJ,CAAC"}
|