@krak-stack/registry 0.1.2 → 0.1.3
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 +17 -1
- package/dist/components/ui/alert.d.ts +10 -0
- package/dist/components/ui/bubble.d.ts +16 -0
- package/dist/components/ui/collapsible.d.ts +5 -0
- package/dist/components/ui/empty.d.ts +11 -0
- package/dist/components/ui/marker.d.ts +10 -0
- package/dist/components/ui/message-scroller.d.ts +10 -0
- package/dist/components/ui/message.d.ts +10 -0
- package/dist/lib/docs-ai.d.ts +136 -0
- package/dist/lib/docs-ai.js +310 -0
- package/dist/lib/docs-core.d.ts +681 -0
- package/dist/lib/docs-core.js +2571 -0
- package/dist/lib/httpapi-ai.d.ts +54 -0
- package/dist/lib/httpapi-ai.js +361 -0
- package/dist/lib/httpapi-cli.d.ts +22 -0
- package/dist/lib/httpapi-cli.js +412 -0
- package/dist/lib/httpapi-client.d.ts +20 -0
- package/dist/lib/httpapi-client.js +50 -0
- package/dist/lib/httpapi-helpers.d.ts +105 -0
- package/dist/lib/httpapi-helpers.js +237 -0
- package/dist/lib/httpapi-mcp.d.ts +20 -0
- package/dist/lib/httpapi-mcp.js +366 -0
- package/dist/lib/query.js +128 -0
- package/dist/services/agent/client/atom.d.ts +56 -0
- package/dist/services/agent/client/index.d.ts +2 -0
- package/dist/services/agent/client/index.js +2239 -0
- package/dist/services/agent/client/widget.d.ts +52 -0
- package/dist/services/agent/index.d.ts +111 -0
- package/dist/services/agent/index.js +190 -0
- package/dist/services/agent/schema.d.ts +161 -0
- package/dist/services/agent/schema.js +135 -0
- package/package.json +58 -2
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// ../../src/lib/httpapi-helpers.tsx
|
|
2
|
+
import {
|
|
3
|
+
Context,
|
|
4
|
+
Effect,
|
|
5
|
+
JsonSchema,
|
|
6
|
+
Layer,
|
|
7
|
+
Option,
|
|
8
|
+
Schema,
|
|
9
|
+
SchemaRepresentation
|
|
10
|
+
} from "effect";
|
|
11
|
+
import { HttpApi, OpenApi } from "effect/unstable/httpapi";
|
|
12
|
+
var JsonObjectSchema = Schema.Record(Schema.String, Schema.Unknown).annotate({
|
|
13
|
+
identifier: "HttpJsonObject",
|
|
14
|
+
title: "HTTP JSON object",
|
|
15
|
+
description: "A JSON object passed to an HTTP API operation.",
|
|
16
|
+
examples: [{ id: "example-id" }]
|
|
17
|
+
});
|
|
18
|
+
var JsonObjectFromString = Schema.fromJsonString(JsonObjectSchema);
|
|
19
|
+
var JsonValueFromString = Schema.fromJsonString(Schema.Unknown);
|
|
20
|
+
var JsonSchemaAnnotations = Schema.Struct({
|
|
21
|
+
title: Schema.optional(Schema.String),
|
|
22
|
+
description: Schema.optional(Schema.String),
|
|
23
|
+
examples: Schema.optional(Schema.Array(Schema.Unknown))
|
|
24
|
+
}).annotate({
|
|
25
|
+
identifier: "HttpJsonSchemaAnnotations",
|
|
26
|
+
title: "HTTP JSON Schema annotations",
|
|
27
|
+
description: "JSON Schema annotations surfaced on generated tool inputs."
|
|
28
|
+
});
|
|
29
|
+
var HttpApiToolInputSchema = Schema.Struct({
|
|
30
|
+
body: Schema.optional(Schema.Unknown)
|
|
31
|
+
}).annotate({
|
|
32
|
+
identifier: "HttpApiToolInput",
|
|
33
|
+
title: "HTTP API tool input",
|
|
34
|
+
description: "Input accepted by an HTTP API-backed tool.",
|
|
35
|
+
examples: [{ body: { id: "example-id" } }]
|
|
36
|
+
});
|
|
37
|
+
var HttpApiMethods = [
|
|
38
|
+
"get",
|
|
39
|
+
"post",
|
|
40
|
+
"put",
|
|
41
|
+
"patch",
|
|
42
|
+
"delete"
|
|
43
|
+
];
|
|
44
|
+
var toHttpError = (message, error) => new Error(message, { cause: error });
|
|
45
|
+
var sanitizeHttpName = (name) => name.replace(/[^a-zA-Z0-9_-]/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
46
|
+
var httpApiToolName = (method, path, operation) => {
|
|
47
|
+
const fallback = `${method}_${path.replace(/^\/api\//, "").replace(/[/:{}]/g, "_")}`;
|
|
48
|
+
return (operation.operationId || fallback).replace(/[^a-zA-Z0-9_-]/g, "_").replace(/_+/g, "_").slice(0, 64);
|
|
49
|
+
};
|
|
50
|
+
var httpApiOperations = ({
|
|
51
|
+
spec,
|
|
52
|
+
methods = HttpApiMethods
|
|
53
|
+
}) => {
|
|
54
|
+
const operations = [];
|
|
55
|
+
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
56
|
+
for (const method of methods) {
|
|
57
|
+
const operation = pathItem[method];
|
|
58
|
+
if (operation)
|
|
59
|
+
operations.push({ method, path, operation });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return operations;
|
|
63
|
+
};
|
|
64
|
+
var httpApiToolEntries = Effect.fn("Http.toolEntries")(function* (operations) {
|
|
65
|
+
return yield* Effect.try({
|
|
66
|
+
try: () => {
|
|
67
|
+
const names = new Set;
|
|
68
|
+
return operations.map((entry) => {
|
|
69
|
+
const name = httpApiToolName(entry.method, entry.path, entry.operation);
|
|
70
|
+
if (!name || names.has(name)) {
|
|
71
|
+
throw new Error(`Duplicate or empty HTTP API tool name: ${name}`);
|
|
72
|
+
}
|
|
73
|
+
names.add(name);
|
|
74
|
+
return { ...entry, name };
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
catch: (error) => error instanceof Error ? error : new Error(String(error))
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
var decodeAnnotations = Schema.decodeUnknownOption(JsonSchemaAnnotations);
|
|
81
|
+
var schemaWithVisibleAnnotations = (schema) => {
|
|
82
|
+
const directAnnotations = decodeAnnotations(schema).pipe(Option.getOrElse(() => ({})));
|
|
83
|
+
const allOf = Array.isArray(schema.allOf) ? schema.allOf : [];
|
|
84
|
+
const annotations = allOf.reduce((acc, item) => ({
|
|
85
|
+
...acc,
|
|
86
|
+
...decodeAnnotations(item).pipe(Option.getOrElse(() => ({})))
|
|
87
|
+
}), directAnnotations);
|
|
88
|
+
return {
|
|
89
|
+
...schema,
|
|
90
|
+
..."title" in annotations && !("title" in schema) ? { title: annotations.title } : {},
|
|
91
|
+
..."description" in annotations && !("description" in schema) ? { description: annotations.description } : {},
|
|
92
|
+
..."examples" in annotations && !("examples" in schema) ? { examples: annotations.examples } : {}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
var referencedDefinitions = (schema, definitions) => {
|
|
96
|
+
const names = new Set;
|
|
97
|
+
const pending = [schema];
|
|
98
|
+
while (pending.length > 0) {
|
|
99
|
+
const current = pending.pop();
|
|
100
|
+
if (!current || typeof current !== "object")
|
|
101
|
+
continue;
|
|
102
|
+
if (Array.isArray(current)) {
|
|
103
|
+
pending.push(...current);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
for (const [key, value] of Object.entries(current)) {
|
|
107
|
+
if (key === "$ref" && typeof value === "string") {
|
|
108
|
+
const name = value.match(/^#\/(?:\$defs|components\/schemas)\/(.+)$/)?.[1];
|
|
109
|
+
if (name && !names.has(name) && definitions[name]) {
|
|
110
|
+
names.add(name);
|
|
111
|
+
pending.push(definitions[name]);
|
|
112
|
+
}
|
|
113
|
+
} else if (key !== "$defs") {
|
|
114
|
+
pending.push(value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return Object.fromEntries([...names].map((name) => [name, definitions[name]]));
|
|
119
|
+
};
|
|
120
|
+
var operationParameters = (parameters, location) => parameters.filter((parameter) => parameter.in === location);
|
|
121
|
+
var httpApiOperationInputSchema = (operation) => {
|
|
122
|
+
const parameters = operation.parameters ?? [];
|
|
123
|
+
const pathParameters = operationParameters(parameters, "path");
|
|
124
|
+
const queryParameters = operationParameters(parameters, "query");
|
|
125
|
+
const headerParameters = operationParameters(parameters, "header");
|
|
126
|
+
const properties = {};
|
|
127
|
+
const required = [];
|
|
128
|
+
const body = operation.requestBody?.content?.["application/json"]?.schema;
|
|
129
|
+
for (const parameter of [
|
|
130
|
+
...pathParameters,
|
|
131
|
+
...queryParameters,
|
|
132
|
+
...headerParameters
|
|
133
|
+
]) {
|
|
134
|
+
properties[parameter.name] = {
|
|
135
|
+
...parameter.schema ? schemaWithVisibleAnnotations(parameter.schema) : { type: "string" },
|
|
136
|
+
...parameter.description ? { description: parameter.description } : {}
|
|
137
|
+
};
|
|
138
|
+
if (parameter.required)
|
|
139
|
+
required.push(parameter.name);
|
|
140
|
+
}
|
|
141
|
+
if (body) {
|
|
142
|
+
properties.body = body;
|
|
143
|
+
if (operation.requestBody?.required)
|
|
144
|
+
required.push("body");
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
type: "object",
|
|
148
|
+
properties,
|
|
149
|
+
required,
|
|
150
|
+
additionalProperties: false
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
var decodeJsonObject = Schema.decodeUnknownOption(JsonObjectSchema);
|
|
154
|
+
var decodeHttpApiOperationInput = Effect.fn("Http.decodeApiOperationInput")(function* (input, operation) {
|
|
155
|
+
const payload = yield* Schema.decodeUnknownEffect(JsonObjectSchema)(input ?? {}).pipe(Effect.mapError(() => new Error("Tool input must be a JSON object")));
|
|
156
|
+
const decoded = yield* Schema.decodeUnknownEffect(HttpApiToolInputSchema)(payload).pipe(Effect.mapError(() => new Error("Tool input must be a JSON object")));
|
|
157
|
+
const parameters = operation.parameters ?? [];
|
|
158
|
+
const pickParameters = (location) => {
|
|
159
|
+
const nestedKey = location === "path" ? "params" : location;
|
|
160
|
+
const nested = decodeJsonObject(payload[nestedKey]).pipe(Option.getOrElse(() => ({})));
|
|
161
|
+
return Object.fromEntries(operationParameters(parameters, location).map((parameter) => [
|
|
162
|
+
parameter.name,
|
|
163
|
+
nested[parameter.name] ?? payload[parameter.name]
|
|
164
|
+
]).filter(([, value]) => value !== undefined));
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
...decoded,
|
|
168
|
+
headers: pickParameters("header"),
|
|
169
|
+
params: pickParameters("path"),
|
|
170
|
+
query: pickParameters("query")
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
var parseJsonObject = Effect.fn("Http.parseJsonObject")(function* (value, label) {
|
|
174
|
+
if (!value)
|
|
175
|
+
return {};
|
|
176
|
+
return yield* Schema.decodeUnknownEffect(JsonObjectFromString)(value).pipe(Effect.mapError(() => new Error(`${label} must be a JSON object`)));
|
|
177
|
+
});
|
|
178
|
+
var parseJsonValue = Effect.fn("Http.parseJsonValue")(function* (value, label) {
|
|
179
|
+
if (!value)
|
|
180
|
+
return;
|
|
181
|
+
return yield* Schema.decodeUnknownEffect(JsonValueFromString)(value).pipe(Effect.mapError(() => new Error(`${label} must be valid JSON`)));
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
class HttpApiSpec extends Context.Service()("HttpApiSpec", {
|
|
185
|
+
make: (config) => Effect.try({
|
|
186
|
+
try: () => {
|
|
187
|
+
if (!HttpApi.isHttpApi(config.api)) {
|
|
188
|
+
throw new Error("HttpApiSpec requires a valid HttpApi");
|
|
189
|
+
}
|
|
190
|
+
const spec = OpenApi.fromApi(config.api);
|
|
191
|
+
const operations = httpApiOperations({
|
|
192
|
+
spec,
|
|
193
|
+
methods: config.methods
|
|
194
|
+
}).filter((operation) => config.include?.(operation) ?? true);
|
|
195
|
+
const operationJsonSchema = (operation) => {
|
|
196
|
+
const schema = httpApiOperationInputSchema(operation);
|
|
197
|
+
const document = JsonSchema.fromSchemaOpenApi3_1({
|
|
198
|
+
...schema,
|
|
199
|
+
$defs: spec.components?.schemas
|
|
200
|
+
});
|
|
201
|
+
const definitions = document.definitions ?? {};
|
|
202
|
+
const usedDefinitions = referencedDefinitions(document.schema, definitions);
|
|
203
|
+
return {
|
|
204
|
+
...document.schema,
|
|
205
|
+
...Object.keys(usedDefinitions).length > 0 ? { $defs: usedDefinitions } : {}
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
info: spec.info,
|
|
210
|
+
operations,
|
|
211
|
+
decodeOperationInput: decodeHttpApiOperationInput,
|
|
212
|
+
operationJsonSchema,
|
|
213
|
+
operationSchema: (operation) => {
|
|
214
|
+
const document = JsonSchema.fromSchemaOpenApi3_1(operationJsonSchema(operation));
|
|
215
|
+
return SchemaRepresentation.toSchema(SchemaRepresentation.fromJsonSchemaDocument(document));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
catch: (error) => toHttpError("Failed to build HTTP API spec", error)
|
|
220
|
+
})
|
|
221
|
+
}) {
|
|
222
|
+
static layer = (config) => Layer.effect(this, this.make(config));
|
|
223
|
+
}
|
|
224
|
+
export {
|
|
225
|
+
toHttpError,
|
|
226
|
+
sanitizeHttpName,
|
|
227
|
+
parseJsonValue,
|
|
228
|
+
parseJsonObject,
|
|
229
|
+
httpApiToolName,
|
|
230
|
+
httpApiToolEntries,
|
|
231
|
+
httpApiOperations,
|
|
232
|
+
httpApiOperationInputSchema,
|
|
233
|
+
decodeHttpApiOperationInput,
|
|
234
|
+
JsonObjectSchema,
|
|
235
|
+
HttpApiSpec,
|
|
236
|
+
HttpApiMethods
|
|
237
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Context, Effect, Layer } from "effect";
|
|
2
|
+
import { McpSchema, McpServer } from "effect/unstable/ai";
|
|
3
|
+
import { ApiClient } from "./httpapi-client.js";
|
|
4
|
+
import { HttpApiSpec } from "./httpapi-helpers.js";
|
|
5
|
+
export type HttpApiMcpConfig = {
|
|
6
|
+
readonly toolMetaKey?: string;
|
|
7
|
+
};
|
|
8
|
+
declare const HttpApiMcp_base: Context.ServiceClass<HttpApiMcp, "HttpApiMcp", {
|
|
9
|
+
registerTools: Effect.Effect<void, Error, McpServer.McpServer>;
|
|
10
|
+
}> & {
|
|
11
|
+
readonly make: (config: HttpApiMcpConfig) => Effect.Effect<{
|
|
12
|
+
registerTools: Effect.Effect<void, Error, McpServer.McpServer>;
|
|
13
|
+
}, never, ApiClient | HttpApiSpec>;
|
|
14
|
+
};
|
|
15
|
+
export declare class HttpApiMcp extends HttpApiMcp_base {
|
|
16
|
+
static readonly layer: (config: HttpApiMcpConfig) => Layer.Layer<HttpApiMcp, never, ApiClient | HttpApiSpec>;
|
|
17
|
+
}
|
|
18
|
+
export declare const httpApiMcpToolsLayer: Layer.Layer<never, Error, HttpApiMcp | McpServer.McpServer>;
|
|
19
|
+
export declare const httpApiMcpServerLayer: (path: Parameters<typeof McpServer.layerHttp>[0]["path"]) => Layer.Layer<McpServer.McpServer | McpSchema.McpServerClient, never, HttpApiSpec | import("effect/unstable/http/HttpRouter").HttpRouter>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// ../../src/lib/httpapi-mcp.ts
|
|
2
|
+
import { Context as Context3, Effect as Effect3, Layer as Layer3, Option as Option2, Schema as Schema2 } from "effect";
|
|
3
|
+
import { McpSchema, McpServer } from "effect/unstable/ai";
|
|
4
|
+
|
|
5
|
+
// ../../src/lib/httpapi-client.ts
|
|
6
|
+
import { Context, Effect, Layer } from "effect";
|
|
7
|
+
import { HttpClient } from "effect/unstable/http";
|
|
8
|
+
import {
|
|
9
|
+
HttpApiClient as EffectHttpApiClient
|
|
10
|
+
} from "effect/unstable/httpapi";
|
|
11
|
+
var makeGeneratedClient = (api, http, baseUrl) => EffectHttpApiClient.makeWith(api, {
|
|
12
|
+
baseUrl,
|
|
13
|
+
httpClient: http
|
|
14
|
+
});
|
|
15
|
+
var isClientEffect = (value) => Effect.isEffect(value);
|
|
16
|
+
var executeGeneratedOperation = Effect.fn("HttpApiClient.executeGeneratedOperation")(function* (client, { input, operation: entry }) {
|
|
17
|
+
const operationId = entry.operation.operationId;
|
|
18
|
+
if (!operationId) {
|
|
19
|
+
return yield* Effect.fail(new Error(`No generated API client operation for ${entry.method} ${entry.path}`));
|
|
20
|
+
}
|
|
21
|
+
const target = Object(client);
|
|
22
|
+
const groupName = Object.keys(target).filter((name) => operationId.startsWith(`${name}.`)).sort((a, b) => b.length - a.length)[0];
|
|
23
|
+
const group = groupName ? Reflect.get(target, groupName) : target;
|
|
24
|
+
const endpointName = groupName ? operationId.slice(groupName.length + 1) : operationId;
|
|
25
|
+
const endpoint = Reflect.get(Object(group), endpointName);
|
|
26
|
+
if (typeof endpoint !== "function") {
|
|
27
|
+
return yield* Effect.fail(new Error(`No generated API client operation for ${operationId}`));
|
|
28
|
+
}
|
|
29
|
+
const result = endpoint({
|
|
30
|
+
headers: input.headers,
|
|
31
|
+
params: input.params,
|
|
32
|
+
query: input.query,
|
|
33
|
+
payload: input.body
|
|
34
|
+
});
|
|
35
|
+
if (!isClientEffect(result)) {
|
|
36
|
+
return yield* Effect.fail(new Error(`Generated API client operation ${operationId} is invalid`));
|
|
37
|
+
}
|
|
38
|
+
return yield* result;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
class ApiClient extends Context.Service()("ApiClient") {
|
|
42
|
+
static layer = (config) => Layer.effect(this, Effect.gen(function* () {
|
|
43
|
+
const http = yield* HttpClient.HttpClient;
|
|
44
|
+
const client = yield* makeGeneratedClient(config.api, http, config.baseUrl);
|
|
45
|
+
return {
|
|
46
|
+
execute: Effect.fn("ApiClient.execute")(function* (options) {
|
|
47
|
+
return yield* executeGeneratedOperation(client, options);
|
|
48
|
+
})
|
|
49
|
+
};
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ../../src/lib/httpapi-helpers.tsx
|
|
54
|
+
import {
|
|
55
|
+
Context as Context2,
|
|
56
|
+
Effect as Effect2,
|
|
57
|
+
JsonSchema,
|
|
58
|
+
Layer as Layer2,
|
|
59
|
+
Option,
|
|
60
|
+
Schema,
|
|
61
|
+
SchemaRepresentation
|
|
62
|
+
} from "effect";
|
|
63
|
+
import { HttpApi as HttpApi2, OpenApi } from "effect/unstable/httpapi";
|
|
64
|
+
var JsonObjectSchema = Schema.Record(Schema.String, Schema.Unknown).annotate({
|
|
65
|
+
identifier: "HttpJsonObject",
|
|
66
|
+
title: "HTTP JSON object",
|
|
67
|
+
description: "A JSON object passed to an HTTP API operation.",
|
|
68
|
+
examples: [{ id: "example-id" }]
|
|
69
|
+
});
|
|
70
|
+
var JsonObjectFromString = Schema.fromJsonString(JsonObjectSchema);
|
|
71
|
+
var JsonValueFromString = Schema.fromJsonString(Schema.Unknown);
|
|
72
|
+
var JsonSchemaAnnotations = Schema.Struct({
|
|
73
|
+
title: Schema.optional(Schema.String),
|
|
74
|
+
description: Schema.optional(Schema.String),
|
|
75
|
+
examples: Schema.optional(Schema.Array(Schema.Unknown))
|
|
76
|
+
}).annotate({
|
|
77
|
+
identifier: "HttpJsonSchemaAnnotations",
|
|
78
|
+
title: "HTTP JSON Schema annotations",
|
|
79
|
+
description: "JSON Schema annotations surfaced on generated tool inputs."
|
|
80
|
+
});
|
|
81
|
+
var HttpApiToolInputSchema = Schema.Struct({
|
|
82
|
+
body: Schema.optional(Schema.Unknown)
|
|
83
|
+
}).annotate({
|
|
84
|
+
identifier: "HttpApiToolInput",
|
|
85
|
+
title: "HTTP API tool input",
|
|
86
|
+
description: "Input accepted by an HTTP API-backed tool.",
|
|
87
|
+
examples: [{ body: { id: "example-id" } }]
|
|
88
|
+
});
|
|
89
|
+
var HttpApiMethods = [
|
|
90
|
+
"get",
|
|
91
|
+
"post",
|
|
92
|
+
"put",
|
|
93
|
+
"patch",
|
|
94
|
+
"delete"
|
|
95
|
+
];
|
|
96
|
+
var toHttpError = (message, error) => new Error(message, { cause: error });
|
|
97
|
+
var httpApiToolName = (method, path, operation) => {
|
|
98
|
+
const fallback = `${method}_${path.replace(/^\/api\//, "").replace(/[/:{}]/g, "_")}`;
|
|
99
|
+
return (operation.operationId || fallback).replace(/[^a-zA-Z0-9_-]/g, "_").replace(/_+/g, "_").slice(0, 64);
|
|
100
|
+
};
|
|
101
|
+
var httpApiOperations = ({
|
|
102
|
+
spec,
|
|
103
|
+
methods = HttpApiMethods
|
|
104
|
+
}) => {
|
|
105
|
+
const operations = [];
|
|
106
|
+
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
107
|
+
for (const method of methods) {
|
|
108
|
+
const operation = pathItem[method];
|
|
109
|
+
if (operation)
|
|
110
|
+
operations.push({ method, path, operation });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return operations;
|
|
114
|
+
};
|
|
115
|
+
var httpApiToolEntries = Effect2.fn("Http.toolEntries")(function* (operations) {
|
|
116
|
+
return yield* Effect2.try({
|
|
117
|
+
try: () => {
|
|
118
|
+
const names = new Set;
|
|
119
|
+
return operations.map((entry) => {
|
|
120
|
+
const name = httpApiToolName(entry.method, entry.path, entry.operation);
|
|
121
|
+
if (!name || names.has(name)) {
|
|
122
|
+
throw new Error(`Duplicate or empty HTTP API tool name: ${name}`);
|
|
123
|
+
}
|
|
124
|
+
names.add(name);
|
|
125
|
+
return { ...entry, name };
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
catch: (error) => error instanceof Error ? error : new Error(String(error))
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
var decodeAnnotations = Schema.decodeUnknownOption(JsonSchemaAnnotations);
|
|
132
|
+
var schemaWithVisibleAnnotations = (schema) => {
|
|
133
|
+
const directAnnotations = decodeAnnotations(schema).pipe(Option.getOrElse(() => ({})));
|
|
134
|
+
const allOf = Array.isArray(schema.allOf) ? schema.allOf : [];
|
|
135
|
+
const annotations = allOf.reduce((acc, item) => ({
|
|
136
|
+
...acc,
|
|
137
|
+
...decodeAnnotations(item).pipe(Option.getOrElse(() => ({})))
|
|
138
|
+
}), directAnnotations);
|
|
139
|
+
return {
|
|
140
|
+
...schema,
|
|
141
|
+
..."title" in annotations && !("title" in schema) ? { title: annotations.title } : {},
|
|
142
|
+
..."description" in annotations && !("description" in schema) ? { description: annotations.description } : {},
|
|
143
|
+
..."examples" in annotations && !("examples" in schema) ? { examples: annotations.examples } : {}
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
var referencedDefinitions = (schema, definitions) => {
|
|
147
|
+
const names = new Set;
|
|
148
|
+
const pending = [schema];
|
|
149
|
+
while (pending.length > 0) {
|
|
150
|
+
const current = pending.pop();
|
|
151
|
+
if (!current || typeof current !== "object")
|
|
152
|
+
continue;
|
|
153
|
+
if (Array.isArray(current)) {
|
|
154
|
+
pending.push(...current);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
for (const [key, value] of Object.entries(current)) {
|
|
158
|
+
if (key === "$ref" && typeof value === "string") {
|
|
159
|
+
const name = value.match(/^#\/(?:\$defs|components\/schemas)\/(.+)$/)?.[1];
|
|
160
|
+
if (name && !names.has(name) && definitions[name]) {
|
|
161
|
+
names.add(name);
|
|
162
|
+
pending.push(definitions[name]);
|
|
163
|
+
}
|
|
164
|
+
} else if (key !== "$defs") {
|
|
165
|
+
pending.push(value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return Object.fromEntries([...names].map((name) => [name, definitions[name]]));
|
|
170
|
+
};
|
|
171
|
+
var operationParameters = (parameters, location) => parameters.filter((parameter) => parameter.in === location);
|
|
172
|
+
var httpApiOperationInputSchema = (operation) => {
|
|
173
|
+
const parameters = operation.parameters ?? [];
|
|
174
|
+
const pathParameters = operationParameters(parameters, "path");
|
|
175
|
+
const queryParameters = operationParameters(parameters, "query");
|
|
176
|
+
const headerParameters = operationParameters(parameters, "header");
|
|
177
|
+
const properties = {};
|
|
178
|
+
const required = [];
|
|
179
|
+
const body = operation.requestBody?.content?.["application/json"]?.schema;
|
|
180
|
+
for (const parameter of [
|
|
181
|
+
...pathParameters,
|
|
182
|
+
...queryParameters,
|
|
183
|
+
...headerParameters
|
|
184
|
+
]) {
|
|
185
|
+
properties[parameter.name] = {
|
|
186
|
+
...parameter.schema ? schemaWithVisibleAnnotations(parameter.schema) : { type: "string" },
|
|
187
|
+
...parameter.description ? { description: parameter.description } : {}
|
|
188
|
+
};
|
|
189
|
+
if (parameter.required)
|
|
190
|
+
required.push(parameter.name);
|
|
191
|
+
}
|
|
192
|
+
if (body) {
|
|
193
|
+
properties.body = body;
|
|
194
|
+
if (operation.requestBody?.required)
|
|
195
|
+
required.push("body");
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
type: "object",
|
|
199
|
+
properties,
|
|
200
|
+
required,
|
|
201
|
+
additionalProperties: false
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
var decodeJsonObject = Schema.decodeUnknownOption(JsonObjectSchema);
|
|
205
|
+
var decodeHttpApiOperationInput = Effect2.fn("Http.decodeApiOperationInput")(function* (input, operation) {
|
|
206
|
+
const payload = yield* Schema.decodeUnknownEffect(JsonObjectSchema)(input ?? {}).pipe(Effect2.mapError(() => new Error("Tool input must be a JSON object")));
|
|
207
|
+
const decoded = yield* Schema.decodeUnknownEffect(HttpApiToolInputSchema)(payload).pipe(Effect2.mapError(() => new Error("Tool input must be a JSON object")));
|
|
208
|
+
const parameters = operation.parameters ?? [];
|
|
209
|
+
const pickParameters = (location) => {
|
|
210
|
+
const nestedKey = location === "path" ? "params" : location;
|
|
211
|
+
const nested = decodeJsonObject(payload[nestedKey]).pipe(Option.getOrElse(() => ({})));
|
|
212
|
+
return Object.fromEntries(operationParameters(parameters, location).map((parameter) => [
|
|
213
|
+
parameter.name,
|
|
214
|
+
nested[parameter.name] ?? payload[parameter.name]
|
|
215
|
+
]).filter(([, value]) => value !== undefined));
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
...decoded,
|
|
219
|
+
headers: pickParameters("header"),
|
|
220
|
+
params: pickParameters("path"),
|
|
221
|
+
query: pickParameters("query")
|
|
222
|
+
};
|
|
223
|
+
});
|
|
224
|
+
var parseJsonObject = Effect2.fn("Http.parseJsonObject")(function* (value, label) {
|
|
225
|
+
if (!value)
|
|
226
|
+
return {};
|
|
227
|
+
return yield* Schema.decodeUnknownEffect(JsonObjectFromString)(value).pipe(Effect2.mapError(() => new Error(`${label} must be a JSON object`)));
|
|
228
|
+
});
|
|
229
|
+
var parseJsonValue = Effect2.fn("Http.parseJsonValue")(function* (value, label) {
|
|
230
|
+
if (!value)
|
|
231
|
+
return;
|
|
232
|
+
return yield* Schema.decodeUnknownEffect(JsonValueFromString)(value).pipe(Effect2.mapError(() => new Error(`${label} must be valid JSON`)));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
class HttpApiSpec extends Context2.Service()("HttpApiSpec", {
|
|
236
|
+
make: (config) => Effect2.try({
|
|
237
|
+
try: () => {
|
|
238
|
+
if (!HttpApi2.isHttpApi(config.api)) {
|
|
239
|
+
throw new Error("HttpApiSpec requires a valid HttpApi");
|
|
240
|
+
}
|
|
241
|
+
const spec = OpenApi.fromApi(config.api);
|
|
242
|
+
const operations = httpApiOperations({
|
|
243
|
+
spec,
|
|
244
|
+
methods: config.methods
|
|
245
|
+
}).filter((operation) => config.include?.(operation) ?? true);
|
|
246
|
+
const operationJsonSchema = (operation) => {
|
|
247
|
+
const schema = httpApiOperationInputSchema(operation);
|
|
248
|
+
const document = JsonSchema.fromSchemaOpenApi3_1({
|
|
249
|
+
...schema,
|
|
250
|
+
$defs: spec.components?.schemas
|
|
251
|
+
});
|
|
252
|
+
const definitions = document.definitions ?? {};
|
|
253
|
+
const usedDefinitions = referencedDefinitions(document.schema, definitions);
|
|
254
|
+
return {
|
|
255
|
+
...document.schema,
|
|
256
|
+
...Object.keys(usedDefinitions).length > 0 ? { $defs: usedDefinitions } : {}
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
info: spec.info,
|
|
261
|
+
operations,
|
|
262
|
+
decodeOperationInput: decodeHttpApiOperationInput,
|
|
263
|
+
operationJsonSchema,
|
|
264
|
+
operationSchema: (operation) => {
|
|
265
|
+
const document = JsonSchema.fromSchemaOpenApi3_1(operationJsonSchema(operation));
|
|
266
|
+
return SchemaRepresentation.toSchema(SchemaRepresentation.fromJsonSchemaDocument(document));
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
},
|
|
270
|
+
catch: (error) => toHttpError("Failed to build HTTP API spec", error)
|
|
271
|
+
})
|
|
272
|
+
}) {
|
|
273
|
+
static layer = (config) => Layer2.effect(this, this.make(config));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ../../src/lib/httpapi-mcp.ts
|
|
277
|
+
class HttpApiMcp extends Context3.Service()("HttpApiMcp", {
|
|
278
|
+
make: (config) => Effect3.gen(function* () {
|
|
279
|
+
const spec = yield* HttpApiSpec;
|
|
280
|
+
const client = yield* ApiClient;
|
|
281
|
+
return {
|
|
282
|
+
registerTools: registerHttpApiTools(spec, client, config)
|
|
283
|
+
};
|
|
284
|
+
})
|
|
285
|
+
}) {
|
|
286
|
+
static layer = (config) => Layer3.effect(this, this.make(config));
|
|
287
|
+
}
|
|
288
|
+
var decodeStructuredContent = Schema2.decodeUnknownOption(JsonObjectSchema);
|
|
289
|
+
var structuredContent = (value) => decodeStructuredContent(value).pipe(Option2.getOrElse(() => ({ result: value ?? null })));
|
|
290
|
+
var toolResult = (value) => new McpSchema.CallToolResult({
|
|
291
|
+
isError: false,
|
|
292
|
+
structuredContent: structuredContent(value),
|
|
293
|
+
content: [{ type: "text", text: JSON.stringify(value, null, 2) ?? "null" }]
|
|
294
|
+
});
|
|
295
|
+
var toolError = (error) => new McpSchema.CallToolResult({
|
|
296
|
+
isError: true,
|
|
297
|
+
content: [
|
|
298
|
+
{
|
|
299
|
+
type: "text",
|
|
300
|
+
text: error instanceof Error ? error.message : String(error)
|
|
301
|
+
}
|
|
302
|
+
]
|
|
303
|
+
});
|
|
304
|
+
var executeOperation = Effect3.fn("HttpApiMcp.executeOperation")(function* (method, path, operation, input, spec, client) {
|
|
305
|
+
const payload = yield* spec.decodeOperationInput(input, operation);
|
|
306
|
+
return yield* client.execute({
|
|
307
|
+
operation: { method, path, operation },
|
|
308
|
+
input: {
|
|
309
|
+
body: payload.body,
|
|
310
|
+
headers: payload.headers,
|
|
311
|
+
params: payload.params,
|
|
312
|
+
query: payload.query
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
var registerOperation = (method, path, operation, name, config, spec, client) => Effect3.gen(function* () {
|
|
317
|
+
const server = yield* McpServer.McpServer;
|
|
318
|
+
yield* server.addTool({
|
|
319
|
+
tool: new McpSchema.Tool({
|
|
320
|
+
name,
|
|
321
|
+
title: operation.summary,
|
|
322
|
+
description: operation.description ?? operation.summary,
|
|
323
|
+
inputSchema: spec.operationJsonSchema(operation),
|
|
324
|
+
annotations: {
|
|
325
|
+
readOnlyHint: method.toUpperCase() === "GET",
|
|
326
|
+
destructiveHint: method === "delete",
|
|
327
|
+
idempotentHint: method === "get" || method === "put" || method === "delete",
|
|
328
|
+
openWorldHint: false
|
|
329
|
+
},
|
|
330
|
+
_meta: {
|
|
331
|
+
[config.toolMetaKey ?? "api/operation"]: {
|
|
332
|
+
method: method.toUpperCase(),
|
|
333
|
+
path
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}),
|
|
337
|
+
annotations: Context3.empty(),
|
|
338
|
+
handle: (payload) => executeOperation(method, path, operation, payload, spec, client).pipe(Effect3.match({
|
|
339
|
+
onFailure: toolError,
|
|
340
|
+
onSuccess: toolResult
|
|
341
|
+
}))
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
var registerHttpApiTools = (spec, client, config) => Effect3.gen(function* () {
|
|
345
|
+
const entries = yield* httpApiToolEntries(spec.operations);
|
|
346
|
+
for (const { method, name, operation, path } of entries) {
|
|
347
|
+
yield* registerOperation(method, path, operation, name, config, spec, client);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
var httpApiMcpToolsLayer = Layer3.effectDiscard(Effect3.gen(function* () {
|
|
351
|
+
const mcp = yield* HttpApiMcp;
|
|
352
|
+
yield* mcp.registerTools;
|
|
353
|
+
}));
|
|
354
|
+
var httpApiMcpServerLayer = (path) => Layer3.unwrap(Effect3.gen(function* () {
|
|
355
|
+
const spec = yield* HttpApiSpec;
|
|
356
|
+
return McpServer.layerHttp({
|
|
357
|
+
name: spec.info.title,
|
|
358
|
+
version: spec.info.version,
|
|
359
|
+
path
|
|
360
|
+
});
|
|
361
|
+
}));
|
|
362
|
+
export {
|
|
363
|
+
httpApiMcpToolsLayer,
|
|
364
|
+
httpApiMcpServerLayer,
|
|
365
|
+
HttpApiMcp
|
|
366
|
+
};
|