create-stitchkit 0.6.23 → 0.7.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.
@@ -1,173 +0,0 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { join } from 'node:path';
3
- import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
4
- import { mountAgent } from 'stitchkit/tools/agent';
5
- import { z } from 'zod';
6
- import { createSurface } from '../packages/backend/src/surface';
7
- import {
8
- assertManifestMatchesSnapshot,
9
- assertSurfaceConformance,
10
- buildSurfaceManifest,
11
- type SurfaceManifestOperation,
12
- } from '../packages/backend/src/surface-manifest';
13
-
14
- export const SURFACE_SNAPSHOT_PATH = 'packages/backend/src/surface.snapshot.json';
15
-
16
- export interface SurfaceProbe {
17
- name: string;
18
- run: () => Promise<void>;
19
- }
20
-
21
- export function defineSurfaceProbe<TInput, TOutput>({
22
- name,
23
- input,
24
- fixture,
25
- output,
26
- run,
27
- }: {
28
- name: string;
29
- input: z.ZodType<TInput>;
30
- fixture: unknown;
31
- output?: z.ZodType<TOutput>;
32
- run: (input: TInput) => Promise<TOutput>;
33
- }): SurfaceProbe {
34
- return {
35
- name,
36
- run: async () => {
37
- const result = await run(input.parse(fixture));
38
- output?.parse(result);
39
- },
40
- };
41
- }
42
-
43
- interface SurfaceConformanceOptions {
44
- apiOrigin: string;
45
- probes?: readonly SurfaceProbe[];
46
- }
47
-
48
- async function readJson(url: string): Promise<unknown> {
49
- const response = await fetch(url, { signal: AbortSignal.timeout(30_000) });
50
- if (!response.ok) throw new Error(`GET ${url} returned ${response.status}`);
51
- return response.json();
52
- }
53
-
54
- /**
55
- * Observe the CLI surface EXTERNALLY — spawn the real CLI process and parse
56
- * its command table, instead of re-deriving the list from the same in-process
57
- * `services` the manifest was built from (which could only ever agree).
58
- */
59
- export async function discoverCliCommands(root = process.cwd()): Promise<string[]> {
60
- const child = Bun.spawn({
61
- cmd: ['bun', 'packages/backend/src/cli.ts', '--help'],
62
- cwd: root,
63
- stdout: 'pipe',
64
- stderr: 'pipe',
65
- });
66
- const [output, errors, exitCode] = await Promise.all([
67
- new Response(child.stdout).text(),
68
- new Response(child.stderr).text(),
69
- child.exited,
70
- ]);
71
- if (exitCode !== 0) {
72
- throw new Error(`CLI --help exited with ${exitCode}: ${errors || output}`);
73
- }
74
- const lines = output.split('\n');
75
- const start = lines.findIndex((line) => line.trim() === 'Commands:');
76
- if (start === -1) throw new Error('CLI --help output carries no "Commands:" section');
77
- const commands: string[] = [];
78
- for (const line of lines.slice(start + 1)) {
79
- if (!line.startsWith(' ')) break;
80
- const name = line.trim().split(/\s+/)[0];
81
- if (name) commands.push(name);
82
- }
83
- return commands;
84
- }
85
-
86
- export async function discoverMcpTools(
87
- apiOrigin: string,
88
- _manifest: readonly SurfaceManifestOperation[],
89
- ): Promise<string[]> {
90
- const client = new Client(
91
- { name: 'surface-conformance', version: '1.0.0' },
92
- { versionNegotiation: { mode: { pin: '2026-07-28' } } },
93
- );
94
- const transport = new StreamableHTTPClientTransport(new URL(`${apiOrigin}/mcp`));
95
- try {
96
- await client.connect(transport);
97
- // Ask the server what it HAS before asking it to list it. A server exposing
98
- // no MCP tool advertises no `tools` capability, and calling `listTools()`
99
- // anyway makes the vendor client log a debug line — noise printed on every
100
- // green run of a gate whose whole output is one line — to return the empty
101
- // list the capability already stated. The absence is still observed here,
102
- // and a manifest that claims an MCP tool still fails against it.
103
- if (!client.getServerCapabilities()?.tools) return [];
104
- return (await client.listTools()).tools.map((tool) => tool.name);
105
- } finally {
106
- await client.close();
107
- }
108
- }
109
-
110
- export async function runSurfaceConformance({
111
- apiOrigin,
112
- probes = [],
113
- }: SurfaceConformanceOptions): Promise<void> {
114
- const { services, socket } = await createSurface();
115
- try {
116
- const manifest = buildSurfaceManifest(services);
117
- // The committed snapshot is the anchor no source edit can move along with
118
- // itself: removing a transport from `expose` changes the live manifest but
119
- // not the snapshot, so the lane fails until the snapshot is deliberately
120
- // regenerated and reviewed.
121
- const snapshot = SurfaceSnapshotSchema.parse(
122
- JSON.parse(await readFile(join(process.cwd(), SURFACE_SNAPSHOT_PATH), 'utf8')),
123
- );
124
- assertManifestMatchesSnapshot(manifest, snapshot);
125
- const openApi = await readJson(`${apiOrigin}/openapi.json`);
126
- const mcpToolNames = await discoverMcpTools(apiOrigin, manifest);
127
- assertSurfaceConformance({
128
- manifest,
129
- openApi: OpenApiSnapshotSchema.parse(openApi),
130
- mcpToolNames,
131
- // AGENT has no external process to observe — it is anchored by the
132
- // snapshot above; CLI is observed by spawning the real CLI.
133
- agentToolNames: Object.keys(mountAgent(services)),
134
- cliToolNames: await discoverCliCommands(),
135
- });
136
- for (const probe of probes) {
137
- try {
138
- await probe.run();
139
- } catch (cause) {
140
- throw new Error(`Surface probe "${probe.name}" failed`, { cause });
141
- }
142
- }
143
- } finally {
144
- await socket.close();
145
- }
146
- }
147
-
148
- const OpenApiSnapshotSchema = z.object({
149
- paths: z.record(z.string(), z.record(z.string(), z.unknown())),
150
- });
151
-
152
- const SurfaceSnapshotSchema: z.ZodType<SurfaceManifestOperation[]> = z.array(
153
- z.object({
154
- service: z.string(),
155
- action: z.string(),
156
- scope: z.string(),
157
- hasInput: z.boolean(),
158
- hasOutput: z.boolean(),
159
- inputShape: z.string().nullable(),
160
- outputShape: z.string().nullable(),
161
- http: z.array(
162
- z.object({
163
- method: z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD']),
164
- path: z.string(),
165
- }),
166
- ),
167
- tools: z.object({
168
- MCP: z.string().optional(),
169
- AGENT: z.string().optional(),
170
- CLI: z.string().optional(),
171
- }),
172
- }),
173
- );