@cubicecho/server-kit 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/_agents/docs/05-graphql.md +3 -2
- package/_agents/docs/07-mcp.md +1 -0
- package/_agents/docs/09-discovery.md +29 -0
- package/dist/discovery/advertiser.d.ts +57 -0
- package/dist/discovery/advertiser.js +69 -0
- package/dist/discovery/graphql.d.ts +40 -0
- package/dist/discovery/graphql.js +24 -0
- package/dist/discovery/index.d.ts +3 -0
- package/dist/discovery/index.js +3 -0
- package/dist/discovery/names.d.ts +51 -0
- package/dist/discovery/names.js +26 -0
- package/dist/graphql/index.d.ts +1 -0
- package/dist/graphql/index.js +1 -0
- package/dist/graphql/introspection.d.ts +28 -0
- package/dist/graphql/introspection.js +71 -0
- package/dist/mcp/discovery.d.ts +30 -0
- package/dist/mcp/discovery.js +24 -0
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.js +1 -0
- package/llms.txt +20 -1
- package/package.json +23 -3
- package/skill/SKILL.md +90 -0
- package/skill/adopting.md +59 -0
- package/skill/auth.md +359 -0
- package/skill/boot.md +327 -0
- package/skill/database.md +103 -0
- package/skill/defaults.md +72 -0
- package/skill/discovery.md +130 -0
- package/skill/graphql.md +153 -0
- package/skill/mcp.md +88 -0
- package/skill/sso.md +187 -0
- package/skill/testing.md +133 -0
package/README.md
CHANGED
|
@@ -33,10 +33,21 @@ try {
|
|
|
33
33
|
|
|
34
34
|
It waits for Postgres, creates the database when it is missing, and refuses a server older than 18, with a message that says what to do.
|
|
35
35
|
|
|
36
|
+
## The agent skill
|
|
37
|
+
|
|
38
|
+
How a server is built on the kit, written for an agent, is the Markdown in
|
|
39
|
+
[`skill/`](https://github.com/cubicecho/server-kit/tree/main/skill): `SKILL.md`, which routes, and one
|
|
40
|
+
file per job (boot, tables and ids, auth, SSO, errors and limits, the MCP door, discovery, defaults,
|
|
41
|
+
tests, adopting the kit). It ships in the package, so `node_modules/@cubicecho/server-kit/skill/` always
|
|
42
|
+
matches the installed version. Put the directory wherever your agent reads skills and keep it whole,
|
|
43
|
+
because the files link each other by relative path. No file is over 25 KB, so a skills server can hand
|
|
44
|
+
each one over whole.
|
|
45
|
+
|
|
36
46
|
## Documentation
|
|
37
47
|
|
|
38
48
|
- [llms.txt](https://cdn.jsdelivr.net/npm/@cubicecho/server-kit/llms.txt)
|
|
39
49
|
- [Pages](https://github.com/cubicecho/server-kit/tree/main/_agents/docs)
|
|
50
|
+
- [The skill](https://github.com/cubicecho/server-kit/blob/main/skill/SKILL.md)
|
|
40
51
|
- [Changelog](https://github.com/cubicecho/server-kit/blob/main/CHANGELOG.md)
|
|
41
52
|
|
|
42
53
|
## License
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
`@cubicecho/server-kit/graphql` has what every Yoga 6 server shares: operation limits, coded errors, input validation and a logger that never prints a request's variables.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
|
-
import { graphqlLogger, REQUEST_ID, useOperationLimits } from '@cubicecho/server-kit/graphql';
|
|
6
|
+
import { graphqlLogger, REQUEST_ID, useIntrospectionGate, useOperationLimits } from '@cubicecho/server-kit/graphql';
|
|
7
7
|
import { createYoga } from 'graphql-yoga';
|
|
8
8
|
|
|
9
9
|
const yoga = createYoga({
|
|
@@ -11,11 +11,12 @@ const yoga = createYoga({
|
|
|
11
11
|
graphqlEndpoint: '/graphql',
|
|
12
12
|
logging: graphqlLogger,
|
|
13
13
|
requestId: REQUEST_ID,
|
|
14
|
-
plugins: [useOperationLimits()],
|
|
14
|
+
plugins: [useOperationLimits(), useIntrospectionGate({ allow: (request) => isPerson(request) })],
|
|
15
15
|
});
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
- `useOperationLimits({ maxCost: 15_000 })` replaces a bound of `OPERATION_LIMIT_DEFAULTS`. It holds subscriptions to them too.
|
|
19
|
+
- `useIntrospectionGate({ allow })` refuses `__schema` and `__type` to every request `allow(request, serverContext)` does not let in, and takes graphql-js's "Did you mean" hints off their errors (`suggestions: true` keeps them). `allow` may be async and is asked only for a document that could introspect. Without it, nobody introspects. Add it in production only. `__typename` is everyone's.
|
|
19
20
|
- Give `buildSchema` the same page sizes: `limits: { defaultLimit: OPERATION_LIMIT_DEFAULTS.defaultPageSize, maxLimit: OPERATION_LIMIT_DEFAULTS.maxPageSize }`.
|
|
20
21
|
- `badInput`, `notFound`, `forbidden`, `conflict`, `unauthenticated` and `rateLimited` build the errors a client reads, by `ErrorCode`. Anything else thrown is masked and logged.
|
|
21
22
|
- `parseOrThrow(schema, value)` takes a zod 4 schema and throws `BAD_USER_INPUT` with its first issue.
|
package/_agents/docs/07-mcp.md
CHANGED
|
@@ -29,4 +29,5 @@ checkMcpBudget(async () => (await client.listTools()).tools);
|
|
|
29
29
|
- `contextFromRequest` takes the context factory /graphql uses, so a key or a session means the same on both doors.
|
|
30
30
|
- `toolOptions` selects one level, allows an explicit null on writes only, drops filters and orders on other tables, and reads read-only and destructive hints from the create, update and delete prefixes. An option written after the spread replaces it.
|
|
31
31
|
- `hints` are keyed by the MCP name (`create_task`), and `include` by the field (`Mutation.createTask`).
|
|
32
|
+
- `mcpEndpoint(names, { endpoint, version, description, auth? })` is the door's AI Catalog entry and Server Card, named by `discoveryNames` from `/discovery` (09-discovery.md). It needs `@cubicecho/mcp-discovery`, an optional peer.
|
|
32
33
|
- `checkMcpBudget` holds one tool to `MCP_DEFAULTS.maxToolSchemaChars` and the listing to `maxToolListChars`. List through a real client, as an agent would.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Discovery
|
|
2
|
+
|
|
3
|
+
`@cubicecho/server-kit/discovery` names a server the way every cubicecho server is named, lists its GraphQL API in its services document, and keeps its `_graphql._tcp` and `_mcp._tcp` announcements in step with a switch the app owns. It needs `@cubicecho/graphql-mdns-discovery`, an optional peer, and `bonjour-service` to announce. The MCP card is `mcpEndpoint`, in `/mcp`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { graphqlDiscoveryMiddleware } from '@cubicecho/graphql-mdns-discovery';
|
|
7
|
+
import { discoveryMiddleware } from '@cubicecho/mcp-discovery';
|
|
8
|
+
import { createLanAdvertiser, discoveryNames, graphqlEndpoint } from '@cubicecho/server-kit/discovery';
|
|
9
|
+
import { mcpEndpoint } from '@cubicecho/server-kit/mcp';
|
|
10
|
+
|
|
11
|
+
const names = discoveryNames({ app: 'shop', title: 'Shop' });
|
|
12
|
+
app.use(graphqlDiscoveryMiddleware(() => [graphqlEndpoint(names, { version, auth: 'Bearer <API key>' })]));
|
|
13
|
+
app.use(discoveryMiddleware(() => [mcpEndpoint(names, { endpoint: 'shop', version, description: 'The shop.' })]));
|
|
14
|
+
|
|
15
|
+
const advertiser = createLanAdvertiser({
|
|
16
|
+
isWanted: () => settings.mdnsAdvertise,
|
|
17
|
+
options: () => ({ name: names.instance, port, mcpPath: '/mcp' }),
|
|
18
|
+
log: (message) => console.log(`[discovery] ${message}`),
|
|
19
|
+
});
|
|
20
|
+
server.listen(port, '0.0.0.0', () => void advertiser.sync());
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `discoveryNames({ app, title, host?, repository? })` gives `instance` (`Shop on nas`), `slug`, `publisher` (`nas.shop`), `repository` (`https://github.com/cubicecho/<app>`) and `serviceName(endpoint)` (`io.github.cubicecho.shop.nas/<endpoint>`). `host` is `os.hostname()` unless given.
|
|
24
|
+
- `graphqlEndpoint(names, { path?, endpoint?, version?, description?, auth?, fields? })` is one entry for `graphqlDiscoveryMiddleware`. `path` is `/graphql` and `endpoint` `graphql` unless given. `auth` describes the `Authorization` header; the value is never in the document. `fields(context)` adds per-request fields, such as subscriptions, and wins over the rest.
|
|
25
|
+
- `mcpEndpoint(names, { path?, endpoint, version, description, auth? })`, from `/mcp`, is one entry for mcp-discovery's `discoveryMiddleware`, with a Server Card whose remote is the URL the request came in on. It needs `@cubicecho/mcp-discovery`.
|
|
26
|
+
- `createLanAdvertiser({ isWanted, options, log, announce? })` announces `_graphql._tcp` and, when `options()` has an `mcpPath`, `_mcp._tcp`, under one name and port. `isWanted` is read on every `sync()`, `options` when an announcement starts. A failed announcement is logged and retried on the next `sync()`. It loads mcp-discovery only to announce an MCP door, and logs how to install it when it is missing. `announce` replaces each type's announcer, for tests.
|
|
27
|
+
- `close()` stops both. Call it first in `stopOnSignals`'s `before`.
|
|
28
|
+
- The kit reads no environment: where the switch lives is the app's choice.
|
|
29
|
+
- Keep one copy of `@cubicecho/discovery-core` (`npm ls @cubicecho/discovery-core`). Two copies are two mDNS responders, and stopping one withdraws the other's address records.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Advertisement, Advertiser } from '@cubicecho/graphql-mdns-discovery/mdns';
|
|
2
|
+
/** What one announcement is started with. */
|
|
3
|
+
export interface Announcement {
|
|
4
|
+
name: string;
|
|
5
|
+
port: number;
|
|
6
|
+
path: string;
|
|
7
|
+
tls?: boolean;
|
|
8
|
+
onError?: (error: Error) => void;
|
|
9
|
+
}
|
|
10
|
+
/** Starts one announcement. */
|
|
11
|
+
export type Announce = (announcement: Announcement) => Promise<Advertisement>;
|
|
12
|
+
/** What is announced, read each time an announcement starts. */
|
|
13
|
+
export interface LanAdvertisement {
|
|
14
|
+
/** The instance name, from `discoveryNames(...).instance`. */
|
|
15
|
+
name: string;
|
|
16
|
+
port: number;
|
|
17
|
+
/**
|
|
18
|
+
* Where the GraphQL API is served.
|
|
19
|
+
*
|
|
20
|
+
* @defaultValue `/graphql`
|
|
21
|
+
*/
|
|
22
|
+
graphqlPath?: string;
|
|
23
|
+
/** Where the MCP door is served. Leave it out, and `_mcp._tcp` is not announced and mcp-discovery never loaded. */
|
|
24
|
+
mcpPath?: string;
|
|
25
|
+
/** Whether the host speaks HTTPS itself. */
|
|
26
|
+
tls?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** What the advertiser needs from the server. */
|
|
29
|
+
export interface LanAdvertiserDeps {
|
|
30
|
+
/**
|
|
31
|
+
* Whether the server is announced, read on every `sync`. The server decides where the switch lives (an env var, a
|
|
32
|
+
* settings row) and calls `sync` again when it changes.
|
|
33
|
+
*/
|
|
34
|
+
isWanted: () => boolean;
|
|
35
|
+
/** What to announce. */
|
|
36
|
+
options: () => LanAdvertisement;
|
|
37
|
+
/** Told when an announcement starts or stops, and why one could not start. */
|
|
38
|
+
log: (message: string) => void;
|
|
39
|
+
/** Replaces how each service type is announced. For tests. */
|
|
40
|
+
announce?: {
|
|
41
|
+
graphql?: Announce;
|
|
42
|
+
mcp?: Announce;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Keeps a server's `_graphql._tcp` announcement, and its `_mcp._tcp` one when it has an MCP door, in step with the
|
|
47
|
+
* server's own switch.
|
|
48
|
+
*
|
|
49
|
+
* @param deps - The switch, what to announce, and the log.
|
|
50
|
+
* @returns `sync()`, called after `listen` and whenever the switch changes, and `close()`, called in `stopOnSignals`'s
|
|
51
|
+
* `before`.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* Both announcements go out under one instance name and port, through the one mDNS responder the discovery packages
|
|
55
|
+
* share. One that fails is logged and the server carries on; the next `sync` tries again.
|
|
56
|
+
*/
|
|
57
|
+
export declare function createLanAdvertiser(deps: LanAdvertiserDeps): Advertiser;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { advertiseGraphql, createGraphqlAdvertiser } from '@cubicecho/graphql-mdns-discovery/mdns';
|
|
2
|
+
/** What an app installs to announce its MCP door. */
|
|
3
|
+
const MCP_DISCOVERY = '@cubicecho/mcp-discovery';
|
|
4
|
+
/**
|
|
5
|
+
* Announces an MCP door through mcp-discovery, loaded on first use so a server without one never needs it.
|
|
6
|
+
*
|
|
7
|
+
* @param announcement - What to announce.
|
|
8
|
+
* @returns The announcement.
|
|
9
|
+
* @throws When mcp-discovery is not installed, saying how to install it.
|
|
10
|
+
*/
|
|
11
|
+
const announceMcp = async (announcement) => {
|
|
12
|
+
let mdns;
|
|
13
|
+
try {
|
|
14
|
+
mdns = await import('@cubicecho/mcp-discovery/mdns');
|
|
15
|
+
}
|
|
16
|
+
catch (cause) {
|
|
17
|
+
throw new Error(`Announcing an MCP door needs ${MCP_DISCOVERY}: npm install ${MCP_DISCOVERY}`, { cause });
|
|
18
|
+
}
|
|
19
|
+
return mdns.advertise(announcement);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Builds one service type's advertiser. Both use graphql-mdns-discovery's, which takes any start function, so an MCP
|
|
23
|
+
* door is kept in step the same way without loading mcp-discovery until it starts.
|
|
24
|
+
*
|
|
25
|
+
* @param deps - The server's switch and log.
|
|
26
|
+
* @param label - Put before each log line, so the two types' lines can be told apart.
|
|
27
|
+
* @param options - What to announce, or null when this type isn't.
|
|
28
|
+
* @param start - Starts this type's announcement.
|
|
29
|
+
* @returns The advertiser.
|
|
30
|
+
*/
|
|
31
|
+
function typeAdvertiser(deps, label, options, start) {
|
|
32
|
+
return createGraphqlAdvertiser({
|
|
33
|
+
isWanted: () => deps.isWanted() && options() !== null,
|
|
34
|
+
// Read only when isWanted is true, so options() is not null here.
|
|
35
|
+
options: () => options(),
|
|
36
|
+
log: (message) => deps.log(`${label}: ${message}`),
|
|
37
|
+
start: (announcement) => start(announcement),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Keeps a server's `_graphql._tcp` announcement, and its `_mcp._tcp` one when it has an MCP door, in step with the
|
|
42
|
+
* server's own switch.
|
|
43
|
+
*
|
|
44
|
+
* @param deps - The switch, what to announce, and the log.
|
|
45
|
+
* @returns `sync()`, called after `listen` and whenever the switch changes, and `close()`, called in `stopOnSignals`'s
|
|
46
|
+
* `before`.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
* Both announcements go out under one instance name and port, through the one mDNS responder the discovery packages
|
|
50
|
+
* share. One that fails is logged and the server carries on; the next `sync` tries again.
|
|
51
|
+
*/
|
|
52
|
+
export function createLanAdvertiser(deps) {
|
|
53
|
+
const graphql = typeAdvertiser(deps, 'GraphQL', () => {
|
|
54
|
+
const { name, port, graphqlPath = '/graphql', tls } = deps.options();
|
|
55
|
+
return { name, port, path: graphqlPath, tls };
|
|
56
|
+
}, deps.announce?.graphql ?? advertiseGraphql);
|
|
57
|
+
const mcp = typeAdvertiser(deps, 'MCP', () => {
|
|
58
|
+
const { name, port, mcpPath, tls } = deps.options();
|
|
59
|
+
return mcpPath === undefined ? null : { name, port, path: mcpPath, tls };
|
|
60
|
+
}, deps.announce?.mcp ?? announceMcp);
|
|
61
|
+
return {
|
|
62
|
+
sync: async () => {
|
|
63
|
+
await Promise.all([graphql.sync(), mcp.sync()]);
|
|
64
|
+
},
|
|
65
|
+
close: async () => {
|
|
66
|
+
await Promise.all([graphql.close(), mcp.close()]);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type AdvertisedGraphqlApi, type GraphqlServiceContext, type GraphqlServiceDraft } from '@cubicecho/graphql-mdns-discovery';
|
|
2
|
+
import type { DiscoveryNames } from './names.ts';
|
|
3
|
+
/** What a server's GraphQL entry says besides its names. */
|
|
4
|
+
export interface GraphqlEndpointOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Where the API is served.
|
|
7
|
+
*
|
|
8
|
+
* @defaultValue `/graphql`
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The endpoint's stable name under the host, the last part of its service name.
|
|
13
|
+
*
|
|
14
|
+
* @defaultValue `graphql`
|
|
15
|
+
*/
|
|
16
|
+
endpoint?: string;
|
|
17
|
+
/** The app's release. */
|
|
18
|
+
version?: string;
|
|
19
|
+
/** One line on what the API is. */
|
|
20
|
+
description?: string;
|
|
21
|
+
/**
|
|
22
|
+
* What a client puts in `Authorization`, never the value itself, such as `Bearer <session token or API key>`.
|
|
23
|
+
* Leave it out for an API anyone may call.
|
|
24
|
+
*/
|
|
25
|
+
auth?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Anything else the entry says, read per request, such as its subscriptions or `schemaUrl`. It wins over the rest.
|
|
28
|
+
*
|
|
29
|
+
* @param context - The request's origin and the API's URL on it.
|
|
30
|
+
*/
|
|
31
|
+
fields?: (context: GraphqlServiceContext) => Partial<GraphqlServiceDraft>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Builds the entry for a server's GraphQL API in its services document.
|
|
35
|
+
*
|
|
36
|
+
* @param names - The server's names, from `discoveryNames`.
|
|
37
|
+
* @param [options] - The path, the endpoint's name, and what the entry says.
|
|
38
|
+
* @returns The entry, for `graphqlDiscoveryMiddleware(() => [entry])`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function graphqlEndpoint(names: DiscoveryNames, options?: GraphqlEndpointOptions): AdvertisedGraphqlApi;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DEFAULT_GRAPHQL_PATH, } from '@cubicecho/graphql-mdns-discovery';
|
|
2
|
+
/**
|
|
3
|
+
* Builds the entry for a server's GraphQL API in its services document.
|
|
4
|
+
*
|
|
5
|
+
* @param names - The server's names, from `discoveryNames`.
|
|
6
|
+
* @param [options] - The path, the endpoint's name, and what the entry says.
|
|
7
|
+
* @returns The entry, for `graphqlDiscoveryMiddleware(() => [entry])`.
|
|
8
|
+
*/
|
|
9
|
+
export function graphqlEndpoint(names, options = {}) {
|
|
10
|
+
const { path = DEFAULT_GRAPHQL_PATH, endpoint = 'graphql', version, description, auth, fields } = options;
|
|
11
|
+
return {
|
|
12
|
+
path,
|
|
13
|
+
service: (context) => ({
|
|
14
|
+
name: names.serviceName(endpoint),
|
|
15
|
+
title: names.instance,
|
|
16
|
+
...(version === undefined ? {} : { version }),
|
|
17
|
+
...(description === undefined ? {} : { description }),
|
|
18
|
+
...(auth === undefined
|
|
19
|
+
? {}
|
|
20
|
+
: { headers: [{ name: 'Authorization', description: auth, isRequired: true, isSecret: true }] }),
|
|
21
|
+
...fields?.(context),
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { type Announce, type Announcement, createLanAdvertiser, type LanAdvertisement, type LanAdvertiserDeps, } from './advertiser.ts';
|
|
2
|
+
export { type GraphqlEndpointOptions, graphqlEndpoint } from './graphql.ts';
|
|
3
|
+
export { type DiscoveryNames, type DiscoveryNamesOptions, discoveryNames, type SourceRepository } from './names.ts';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Where an app's source lives. */
|
|
2
|
+
export interface SourceRepository {
|
|
3
|
+
url: string;
|
|
4
|
+
source: string;
|
|
5
|
+
}
|
|
6
|
+
/** What a server's names are built from. */
|
|
7
|
+
export interface DiscoveryNamesOptions {
|
|
8
|
+
/** The app's package and repository name, such as `ai-router`. */
|
|
9
|
+
app: string;
|
|
10
|
+
/** What people call it, such as `AI Router`. The instance name is this "on" the host. */
|
|
11
|
+
title: string;
|
|
12
|
+
/**
|
|
13
|
+
* The machine's hostname, which keeps two copies of the app on one network apart.
|
|
14
|
+
*
|
|
15
|
+
* @defaultValue `os.hostname()`
|
|
16
|
+
*/
|
|
17
|
+
host?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Where its source lives.
|
|
20
|
+
*
|
|
21
|
+
* @defaultValue `https://github.com/cubicecho/<app>` on GitHub
|
|
22
|
+
*/
|
|
23
|
+
repository?: SourceRepository;
|
|
24
|
+
}
|
|
25
|
+
/** The names one server advertises under, the same for its GraphQL API and its MCP door. */
|
|
26
|
+
export interface DiscoveryNames {
|
|
27
|
+
app: string;
|
|
28
|
+
title: string;
|
|
29
|
+
host: string;
|
|
30
|
+
/** The host, as a DNS label. */
|
|
31
|
+
slug: string;
|
|
32
|
+
/** The mDNS instance name, such as `AI Router on nas`. A server that watches the network skips its own. */
|
|
33
|
+
instance: string;
|
|
34
|
+
/** The `urn:air` publisher of its MCP endpoints, such as `nas.ai-router`. */
|
|
35
|
+
publisher: string;
|
|
36
|
+
repository: SourceRepository;
|
|
37
|
+
/**
|
|
38
|
+
* Names one endpoint, such as `io.github.cubicecho.ai-router.nas/agents`.
|
|
39
|
+
*
|
|
40
|
+
* @param endpoint - The endpoint's stable name under the host.
|
|
41
|
+
* @returns The reverse-DNS name, with exactly one slash.
|
|
42
|
+
*/
|
|
43
|
+
serviceName: (endpoint: string) => string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Builds the names a server advertises under, so every app names itself the same way.
|
|
47
|
+
*
|
|
48
|
+
* @param options - The app, its title, and optionally the hostname and repository.
|
|
49
|
+
* @returns The names.
|
|
50
|
+
*/
|
|
51
|
+
export declare function discoveryNames(options: DiscoveryNamesOptions): DiscoveryNames;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { hostname } from 'node:os';
|
|
2
|
+
import { hostSlug } from '@cubicecho/graphql-mdns-discovery';
|
|
3
|
+
/** The reverse-DNS namespace every cubicecho service name starts with. */
|
|
4
|
+
const NAMESPACE = 'io.github.cubicecho';
|
|
5
|
+
/** Where a cubicecho app's source lives, shown on what it advertises. */
|
|
6
|
+
const GITHUB = 'https://github.com/cubicecho';
|
|
7
|
+
/**
|
|
8
|
+
* Builds the names a server advertises under, so every app names itself the same way.
|
|
9
|
+
*
|
|
10
|
+
* @param options - The app, its title, and optionally the hostname and repository.
|
|
11
|
+
* @returns The names.
|
|
12
|
+
*/
|
|
13
|
+
export function discoveryNames(options) {
|
|
14
|
+
const { app, title, host = hostname() } = options;
|
|
15
|
+
const slug = hostSlug(host);
|
|
16
|
+
return {
|
|
17
|
+
app,
|
|
18
|
+
title,
|
|
19
|
+
host,
|
|
20
|
+
slug,
|
|
21
|
+
instance: `${title} on ${host}`,
|
|
22
|
+
publisher: `${slug}.${app}`,
|
|
23
|
+
repository: options.repository ?? { url: `${GITHUB}/${app}`, source: 'github' },
|
|
24
|
+
serviceName: (endpoint) => `${NAMESPACE}.${app}.${slug}/${endpoint}`,
|
|
25
|
+
};
|
|
26
|
+
}
|
package/dist/graphql/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { OPERATION_LIMIT_DEFAULTS, type OperationLimitSettings } from './defaults.ts';
|
|
2
2
|
export { badInput, conflict, ErrorCode, errorMessage, forbidden, notFound, rateLimited, requireAuth, tooComplex, unauthenticated, } from './errors.ts';
|
|
3
|
+
export { type IntrospectionGateOptions, useIntrospectionGate } from './introspection.ts';
|
|
3
4
|
export { graphqlLogger, REQUEST_ID } from './logger.ts';
|
|
4
5
|
export { assertWithinLimits, type Operation, useOperationLimits } from './operation-limits.ts';
|
|
5
6
|
export { type Parser, parseOrThrow } from './validation.ts';
|
package/dist/graphql/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { OPERATION_LIMIT_DEFAULTS } from './defaults.js';
|
|
2
2
|
export { badInput, conflict, ErrorCode, errorMessage, forbidden, notFound, rateLimited, requireAuth, tooComplex, unauthenticated, } from './errors.js';
|
|
3
|
+
export { useIntrospectionGate } from './introspection.js';
|
|
3
4
|
export { graphqlLogger, REQUEST_ID } from './logger.js';
|
|
4
5
|
export { assertWithinLimits, useOperationLimits } from './operation-limits.js';
|
|
5
6
|
export { parseOrThrow } from './validation.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Plugin, PromiseOrValue } from 'graphql-yoga';
|
|
2
|
+
/** What a gate is given to decide who may introspect. */
|
|
3
|
+
export interface IntrospectionGateOptions<TServerContext> {
|
|
4
|
+
/**
|
|
5
|
+
* Whether this request may introspect. Asked only for a document that could, and once per request.
|
|
6
|
+
* Leave it out and nobody may. Anything it throws fails the request, so return false for a caller it refuses.
|
|
7
|
+
*/
|
|
8
|
+
allow?: (request: Request, serverContext: TServerContext) => PromiseOrValue<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Whether a request not let in still reads graphql-js's "Did you mean" hints, which name fields. Off by default.
|
|
11
|
+
* Only a document that could introspect is let in, so with it off a typo gets no hint from anyone.
|
|
12
|
+
*/
|
|
13
|
+
suggestions?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The Yoga plugin that refuses introspection to everyone `allow` does not let in, so a production server does not
|
|
17
|
+
* hand its schema to whoever asks.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam TServerContext - What the server hands Yoga, such as Express's `req`.
|
|
20
|
+
* @param [options] - Who may introspect, and whether a refused caller reads hints.
|
|
21
|
+
* @returns The plugin, for `createYoga({ plugins })`.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Validation is synchronous and deciding may not be, so `allow` is asked while the request's parameters are read.
|
|
25
|
+
* It fails closed: every request not let in is refused, including one with no query text (a persisted operation) or
|
|
26
|
+
* one that never had its parameters read (a WebSocket). `__typename` is everyone's.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useIntrospectionGate<TServerContext extends object = Record<string, unknown>>(options?: IntrospectionGateOptions<TServerContext>): Plugin<object, TServerContext>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { GraphQLError, NoSchemaIntrospectionCustomRule } from 'graphql';
|
|
2
|
+
/** A document that could introspect. A field name can't be escaped, so this text is in every one that does. */
|
|
3
|
+
const INTROSPECTION = /\b__(?:schema|type)\b/;
|
|
4
|
+
/** graphql-js's hint, always the end of its message. */
|
|
5
|
+
const SUGGESTION = / Did you mean .*\?$/s;
|
|
6
|
+
/**
|
|
7
|
+
* Takes the hint off a validation error, keeping where it points.
|
|
8
|
+
*
|
|
9
|
+
* @param error - A validation error.
|
|
10
|
+
* @returns The error, or a copy without its hint.
|
|
11
|
+
*/
|
|
12
|
+
function withoutSuggestion(error) {
|
|
13
|
+
if (error instanceof GraphQLError === false) {
|
|
14
|
+
return error;
|
|
15
|
+
}
|
|
16
|
+
const message = error.message.replace(SUGGESTION, '');
|
|
17
|
+
if (message === error.message) {
|
|
18
|
+
return error;
|
|
19
|
+
}
|
|
20
|
+
return new GraphQLError(message, {
|
|
21
|
+
nodes: error.nodes,
|
|
22
|
+
source: error.source,
|
|
23
|
+
positions: error.positions,
|
|
24
|
+
path: error.path,
|
|
25
|
+
originalError: error.originalError,
|
|
26
|
+
extensions: error.extensions,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The Yoga plugin that refuses introspection to everyone `allow` does not let in, so a production server does not
|
|
31
|
+
* hand its schema to whoever asks.
|
|
32
|
+
*
|
|
33
|
+
* @typeParam TServerContext - What the server hands Yoga, such as Express's `req`.
|
|
34
|
+
* @param [options] - Who may introspect, and whether a refused caller reads hints.
|
|
35
|
+
* @returns The plugin, for `createYoga({ plugins })`.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* Validation is synchronous and deciding may not be, so `allow` is asked while the request's parameters are read.
|
|
39
|
+
* It fails closed: every request not let in is refused, including one with no query text (a persisted operation) or
|
|
40
|
+
* one that never had its parameters read (a WebSocket). `__typename` is everyone's.
|
|
41
|
+
*/
|
|
42
|
+
export function useIntrospectionGate(options = {}) {
|
|
43
|
+
const { allow, suggestions = false } = options;
|
|
44
|
+
const isLetIn = new WeakSet();
|
|
45
|
+
return {
|
|
46
|
+
onParams: async ({ params, request, context }) => {
|
|
47
|
+
if (allow === undefined || INTROSPECTION.test(params.query ?? '') === false) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (await allow(request, context)) {
|
|
51
|
+
isLetIn.add(request);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
onValidate: ({ context, addValidationRule }) => {
|
|
55
|
+
// Yoga keys its validation cache on the rules, so a person's cached answer is never a refused caller's.
|
|
56
|
+
const request = context.request;
|
|
57
|
+
if (request !== undefined && isLetIn.has(request)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
addValidationRule(NoSchemaIntrospectionCustomRule);
|
|
61
|
+
if (suggestions) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
return ({ result, setResult }) => {
|
|
65
|
+
if (result.length > 0) {
|
|
66
|
+
setResult(result.map(withoutSuggestion));
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type AdvertisedEndpoint } from '@cubicecho/mcp-discovery';
|
|
2
|
+
import type { DiscoveryNames } from '../discovery/names.ts';
|
|
3
|
+
/** What a server's MCP card says besides its names. */
|
|
4
|
+
export interface McpEndpointOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Where the door is served.
|
|
7
|
+
*
|
|
8
|
+
* @defaultValue `/mcp`
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
11
|
+
/** The endpoint's stable name under the host, the last part of its card name and `urn:air` id, such as `agents`. */
|
|
12
|
+
endpoint: string;
|
|
13
|
+
/** The app's release, the card's version. */
|
|
14
|
+
version: string;
|
|
15
|
+
/** What the door offers, in at most 100 characters. Cards are public: name no data behind it. */
|
|
16
|
+
description: string;
|
|
17
|
+
/**
|
|
18
|
+
* What a client puts in `Authorization`, never the value itself, such as `Bearer <workspace key>`. Leave it out for
|
|
19
|
+
* a door anyone may call.
|
|
20
|
+
*/
|
|
21
|
+
auth?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Builds the AI Catalog entry for a server's MCP door, with its Server Card.
|
|
25
|
+
*
|
|
26
|
+
* @param names - The server's names, from `discoveryNames` in `@cubicecho/server-kit/discovery`.
|
|
27
|
+
* @param options - The path, the endpoint's name, the version and what the card says.
|
|
28
|
+
* @returns The entry, for `discoveryMiddleware(() => [entry])`. Its card names the URL the request came in on.
|
|
29
|
+
*/
|
|
30
|
+
export declare function mcpEndpoint(names: DiscoveryNames, options: McpEndpointOptions): AdvertisedEndpoint;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { airIdentifier, createServerCard, TRANSPORT_STREAMABLE_HTTP, } from '@cubicecho/mcp-discovery';
|
|
2
|
+
/**
|
|
3
|
+
* Builds the AI Catalog entry for a server's MCP door, with its Server Card.
|
|
4
|
+
*
|
|
5
|
+
* @param names - The server's names, from `discoveryNames` in `@cubicecho/server-kit/discovery`.
|
|
6
|
+
* @param options - The path, the endpoint's name, the version and what the card says.
|
|
7
|
+
* @returns The entry, for `discoveryMiddleware(() => [entry])`. Its card names the URL the request came in on.
|
|
8
|
+
*/
|
|
9
|
+
export function mcpEndpoint(names, options) {
|
|
10
|
+
const { path = '/mcp', endpoint, version, description, auth } = options;
|
|
11
|
+
const headers = auth === undefined ? undefined : [{ name: 'Authorization', description: auth, isRequired: true, isSecret: true }];
|
|
12
|
+
return {
|
|
13
|
+
path,
|
|
14
|
+
identifier: airIdentifier(names.publisher, endpoint),
|
|
15
|
+
card: ({ endpointUrl }) => createServerCard({
|
|
16
|
+
name: names.serviceName(endpoint),
|
|
17
|
+
version,
|
|
18
|
+
title: names.instance,
|
|
19
|
+
description,
|
|
20
|
+
repository: names.repository,
|
|
21
|
+
remotes: [{ type: TRANSPORT_STREAMABLE_HTTP, url: endpointUrl, ...(headers === undefined ? {} : { headers }) }],
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
}
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { MCP_DEFAULTS, type McpSettings } from './defaults.ts';
|
|
2
|
+
export { type McpEndpointOptions, mcpEndpoint } from './discovery.ts';
|
|
2
3
|
export { contextFromRequest } from './request.ts';
|
|
3
4
|
export { type SharedToolOptions, type ToolOptions, toolOptions } from './tool-options.ts';
|
package/dist/mcp/index.js
CHANGED
package/llms.txt
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Agent-facing docs for @cubicecho/server-kit: one task or concept per page, then what each package.json "exports" entry exports. Generated from _agents/docs/ and the doc comments in src/ by @cubicecho/agent-docs; edit those, not this file.
|
|
6
6
|
|
|
7
|
-
Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optional), `@cubicecho/better-auth-oidc-pairing` ^1.1.0 (optional), `@cubicecho/graphql-mcp` ^3.1.0 (optional), `@electric-sql/pglite` ^0.5.0 (optional), `better-auth` >=1.7.28 <1.8.0 (optional), `drizzle-orm` 1.0.0-rc.5, `express` ^5.0.0, `graphql` ^16.0.0, `graphql-yoga` ^6.0.0, `postgres` ^3.4.0 || ^4.0.0 (optional), `vitest` ^5.0.0 (optional).
|
|
7
|
+
Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optional), `@cubicecho/better-auth-oidc-pairing` ^1.1.0 (optional), `@cubicecho/graphql-mcp` ^3.1.0 (optional), `@cubicecho/graphql-mdns-discovery` ^1.2.0 (optional), `@cubicecho/mcp-discovery` >=0.7.0 <0.9.0 (optional), `@electric-sql/pglite` ^0.5.0 (optional), `better-auth` >=1.7.28 <1.8.0 (optional), `bonjour-service` ^1.4.0 (optional), `drizzle-orm` 1.0.0-rc.5, `express` ^5.0.0, `graphql` ^16.0.0, `graphql-yoga` ^6.0.0, `postgres` ^3.4.0 || ^4.0.0 (optional), `vitest` ^5.0.0 (optional).
|
|
8
8
|
|
|
9
9
|
## Docs
|
|
10
10
|
|
|
@@ -16,6 +16,7 @@ Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optio
|
|
|
16
16
|
- [HTTP](_agents/docs/06-http.md): `@cubicecho/server-kit/http` has what surrounds an Express app: a clean stop, the health check, serving the built app, and the two OIDC redirects.
|
|
17
17
|
- [MCP](_agents/docs/07-mcp.md): `@cubicecho/server-kit/mcp` has what every MCP door over a drizzle-graphql schema shares: how the caller is read, and the options that keep the listing small. It needs `@cubicecho/graphql-mcp`, an optional peer.
|
|
18
18
|
- [Database lock](_agents/docs/08-lock.md): `@cubicecho/server-kit/lock` makes one process the only one that uses a database, for an app that keeps what it serves in memory. It is a Postgres advisory lock held on a connection of its own. It needs no peer: the connection is a `pg.Client` the app makes.
|
|
19
|
+
- [Discovery](_agents/docs/09-discovery.md): `@cubicecho/server-kit/discovery` names a server the way every cubicecho server is named, lists its GraphQL API in its services document, and keeps its `_graphql._tcp` and `_mcp._tcp` announcements in step with a switch the app owns. It needs `@cubicecho/graphql-mdns-discovery`, an optional peer, and `bonjour-service` to announce. The MCP card is `mcpEndpoint`, in `/mcp`.
|
|
19
20
|
- [Common mistakes](_agents/docs/common-mistakes.md): What goes wrong when the kit is wired up slightly differently, and the fix for each.
|
|
20
21
|
|
|
21
22
|
## Exports: @cubicecho/server-kit/auth
|
|
@@ -88,6 +89,20 @@ Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optio
|
|
|
88
89
|
- `Queryable` (type): Anything that can run a query: the Drizzle client on postgres-js, pg or PGlite.
|
|
89
90
|
- `waitForDatabase`: Waits until Postgres answers a query.
|
|
90
91
|
|
|
92
|
+
## Exports: @cubicecho/server-kit/discovery
|
|
93
|
+
|
|
94
|
+
- `Announce` (type): Starts one announcement.
|
|
95
|
+
- `Announcement` (type): What one announcement is started with.
|
|
96
|
+
- `createLanAdvertiser`: Keeps a server's `_graphql._tcp` announcement, and its `_mcp._tcp` one when it has an MCP door, in step with the server's own switch.
|
|
97
|
+
- `LanAdvertisement` (type): What is announced, read each time an announcement starts.
|
|
98
|
+
- `LanAdvertiserDeps` (type): What the advertiser needs from the server.
|
|
99
|
+
- `GraphqlEndpointOptions` (type): What a server's GraphQL entry says besides its names.
|
|
100
|
+
- `graphqlEndpoint`: Builds the entry for a server's GraphQL API in its services document.
|
|
101
|
+
- `DiscoveryNames` (type): The names one server advertises under, the same for its GraphQL API and its MCP door.
|
|
102
|
+
- `DiscoveryNamesOptions` (type): What a server's names are built from.
|
|
103
|
+
- `discoveryNames`: Builds the names a server advertises under, so every app names itself the same way.
|
|
104
|
+
- `SourceRepository` (type): Where an app's source lives.
|
|
105
|
+
|
|
91
106
|
## Exports: @cubicecho/server-kit/graphql
|
|
92
107
|
|
|
93
108
|
- `OPERATION_LIMIT_DEFAULTS`: What `useOperationLimits` and an app's `buildSchema` use when the app overrides nothing.
|
|
@@ -102,6 +117,8 @@ Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optio
|
|
|
102
117
|
- `requireAuth`: Reads the signed-in user's id.
|
|
103
118
|
- `tooComplex`: Builds the refusal of an operation that asks for more than one may.
|
|
104
119
|
- `unauthenticated`: Builds the signed-out refusal.
|
|
120
|
+
- `IntrospectionGateOptions` (type): What a gate is given to decide who may introspect.
|
|
121
|
+
- `useIntrospectionGate`: The Yoga plugin that refuses introspection to everyone `allow` does not let in, so a production server does not hand its schema to whoever asks.
|
|
105
122
|
- `graphqlLogger`: Yoga's logger with the `[graphql]` tag: warnings and errors, each with its request's id.
|
|
106
123
|
- `REQUEST_ID`: Yoga's `requestId` option: each request gets an id of ours, never one the caller chose.
|
|
107
124
|
- `assertWithinLimits`: Holds an operation to the limits.
|
|
@@ -143,6 +160,8 @@ Requires Node >=22. Peer dependencies: `@better-auth/sso` >=1.7.28 <1.8.0 (optio
|
|
|
143
160
|
|
|
144
161
|
- `MCP_DEFAULTS`: What the MCP helpers and `checkMcpBudget` use when the app overrides nothing.
|
|
145
162
|
- `McpSettings` (type): Bounds on the MCP door.
|
|
163
|
+
- `McpEndpointOptions` (type): What a server's MCP card says besides its names.
|
|
164
|
+
- `mcpEndpoint`: Builds the AI Catalog entry for a server's MCP door, with its Server Card.
|
|
146
165
|
- `contextFromRequest`: Reads an MCP request's caller the way /graphql reads its own, so a credential means the same on both doors.
|
|
147
166
|
- `SharedToolOptions` (type): The part of graphql-mcp's options that is the same for every schema drizzle-graphql generates.
|
|
148
167
|
- `ToolOptions` (type): What an app says about its tools.
|