@decocms/runtime 1.1.3 → 1.2.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/package.json +1 -1
- package/src/index.ts +6 -0
- package/src/tools.ts +186 -2
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -22,6 +22,12 @@ export {
|
|
|
22
22
|
type PromptExecutionContext,
|
|
23
23
|
type CreatedPrompt,
|
|
24
24
|
type GetPromptResult,
|
|
25
|
+
createResource,
|
|
26
|
+
createPublicResource,
|
|
27
|
+
type Resource,
|
|
28
|
+
type ResourceExecutionContext,
|
|
29
|
+
type ResourceContents,
|
|
30
|
+
type CreatedResource,
|
|
25
31
|
} from "./tools.ts";
|
|
26
32
|
import type { Binding } from "./wrangler.ts";
|
|
27
33
|
export { proxyConnectionForId, BindingOf } from "./bindings.ts";
|
package/src/tools.ts
CHANGED
|
@@ -133,6 +133,67 @@ export type CreatedPrompt = {
|
|
|
133
133
|
}): Promise<GetPromptResult> | GetPromptResult;
|
|
134
134
|
};
|
|
135
135
|
|
|
136
|
+
// ============================================================================
|
|
137
|
+
// Resource Types
|
|
138
|
+
// ============================================================================
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Context passed to resource read functions.
|
|
142
|
+
*/
|
|
143
|
+
export interface ResourceExecutionContext {
|
|
144
|
+
uri: URL;
|
|
145
|
+
runtimeContext: AppContext;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Resource contents returned from read operations.
|
|
150
|
+
* Per MCP spec, resources return either text or blob content.
|
|
151
|
+
*/
|
|
152
|
+
export interface ResourceContents {
|
|
153
|
+
/** The URI of the resource */
|
|
154
|
+
uri: string;
|
|
155
|
+
/** MIME type of the content */
|
|
156
|
+
mimeType?: string;
|
|
157
|
+
/** Text content (for text-based resources) */
|
|
158
|
+
text?: string;
|
|
159
|
+
/** Base64-encoded binary content (for binary resources) */
|
|
160
|
+
blob?: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Resource interface for defining MCP resources.
|
|
165
|
+
* Resources are read-only, addressable entities that expose data like config, docs, or context.
|
|
166
|
+
*/
|
|
167
|
+
export interface Resource {
|
|
168
|
+
/** Resource URI (static) or URI template (e.g., "config://app" or "file://{path}") */
|
|
169
|
+
uri: string;
|
|
170
|
+
/** Human-readable name for the resource */
|
|
171
|
+
name: string;
|
|
172
|
+
/** Description of what the resource contains */
|
|
173
|
+
description?: string;
|
|
174
|
+
/** MIME type of the resource content */
|
|
175
|
+
mimeType?: string;
|
|
176
|
+
/** Handler function to read the resource content */
|
|
177
|
+
read(
|
|
178
|
+
context: ResourceExecutionContext,
|
|
179
|
+
): Promise<ResourceContents> | ResourceContents;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* CreatedResource is a permissive type that any Resource can be assigned to.
|
|
184
|
+
* Uses a structural type with relaxed read signature to allow resources with any context.
|
|
185
|
+
*/
|
|
186
|
+
export type CreatedResource = {
|
|
187
|
+
uri: string;
|
|
188
|
+
name: string;
|
|
189
|
+
description?: string;
|
|
190
|
+
mimeType?: string;
|
|
191
|
+
read(context: {
|
|
192
|
+
uri: URL;
|
|
193
|
+
runtimeContext: AppContext;
|
|
194
|
+
}): Promise<ResourceContents> | ResourceContents;
|
|
195
|
+
};
|
|
196
|
+
|
|
136
197
|
/**
|
|
137
198
|
* creates a private tool that always ensure for athentication before being executed
|
|
138
199
|
*/
|
|
@@ -223,6 +284,39 @@ export function createPrompt<TArgs extends PromptArgsRawShape>(
|
|
|
223
284
|
});
|
|
224
285
|
}
|
|
225
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Creates a public resource that does not require authentication.
|
|
289
|
+
*/
|
|
290
|
+
export function createPublicResource(opts: Resource): Resource {
|
|
291
|
+
return {
|
|
292
|
+
...opts,
|
|
293
|
+
read: (input: ResourceExecutionContext) => {
|
|
294
|
+
return opts.read({
|
|
295
|
+
...input,
|
|
296
|
+
runtimeContext: createRuntimeContext(input.runtimeContext),
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Creates a resource that always ensures authentication before being read.
|
|
304
|
+
* This is the default and recommended way to create resources.
|
|
305
|
+
*/
|
|
306
|
+
export function createResource(opts: Resource): Resource {
|
|
307
|
+
const read = opts.read;
|
|
308
|
+
return createPublicResource({
|
|
309
|
+
...opts,
|
|
310
|
+
read: (input: ResourceExecutionContext) => {
|
|
311
|
+
const env = input.runtimeContext.env;
|
|
312
|
+
if (env) {
|
|
313
|
+
env.MESH_REQUEST_CONTEXT?.ensureAuthenticated();
|
|
314
|
+
}
|
|
315
|
+
return read(input);
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
226
320
|
export interface ViewExport {
|
|
227
321
|
title: string;
|
|
228
322
|
icon: string;
|
|
@@ -360,6 +454,17 @@ export interface CreateMCPServerOptions<
|
|
|
360
454
|
| Promise<CreatedPrompt[]>
|
|
361
455
|
>
|
|
362
456
|
| ((env: TEnv) => CreatedPrompt[] | Promise<CreatedPrompt[]>);
|
|
457
|
+
resources?:
|
|
458
|
+
| Array<
|
|
459
|
+
(
|
|
460
|
+
env: TEnv,
|
|
461
|
+
) =>
|
|
462
|
+
| Promise<CreatedResource>
|
|
463
|
+
| CreatedResource
|
|
464
|
+
| CreatedResource[]
|
|
465
|
+
| Promise<CreatedResource[]>
|
|
466
|
+
>
|
|
467
|
+
| ((env: TEnv) => CreatedResource[] | Promise<CreatedResource[]>);
|
|
363
468
|
}
|
|
364
469
|
|
|
365
470
|
export type Fetch<TEnv = unknown> = (
|
|
@@ -530,7 +635,7 @@ export const createMCPServer = <
|
|
|
530
635
|
|
|
531
636
|
const server = new McpServer(
|
|
532
637
|
{ name: "@deco/mcp-api", version: "1.0.0" },
|
|
533
|
-
{ capabilities: { tools: {}, prompts: {} } },
|
|
638
|
+
{ capabilities: { tools: {}, prompts: {}, resources: {} } },
|
|
534
639
|
);
|
|
535
640
|
|
|
536
641
|
const toolsFn =
|
|
@@ -637,7 +742,86 @@ export const createMCPServer = <
|
|
|
637
742
|
);
|
|
638
743
|
}
|
|
639
744
|
|
|
640
|
-
|
|
745
|
+
// Resolve and register resources
|
|
746
|
+
const resourcesFn =
|
|
747
|
+
typeof options.resources === "function"
|
|
748
|
+
? options.resources
|
|
749
|
+
: async (bindings: TEnv) => {
|
|
750
|
+
if (typeof options.resources === "function") {
|
|
751
|
+
return await options.resources(bindings);
|
|
752
|
+
}
|
|
753
|
+
return await Promise.all(
|
|
754
|
+
options.resources?.flatMap(async (resource) => {
|
|
755
|
+
const resourceResult = resource(bindings);
|
|
756
|
+
const awaited = await resourceResult;
|
|
757
|
+
if (Array.isArray(awaited)) {
|
|
758
|
+
return awaited;
|
|
759
|
+
}
|
|
760
|
+
return [awaited];
|
|
761
|
+
}) ?? [],
|
|
762
|
+
).then((r) => r.flat());
|
|
763
|
+
};
|
|
764
|
+
const resources = await resourcesFn(bindings);
|
|
765
|
+
|
|
766
|
+
for (const resource of resources) {
|
|
767
|
+
server.resource(
|
|
768
|
+
resource.name,
|
|
769
|
+
resource.uri,
|
|
770
|
+
{
|
|
771
|
+
description: resource.description,
|
|
772
|
+
mimeType: resource.mimeType,
|
|
773
|
+
},
|
|
774
|
+
async (uri) => {
|
|
775
|
+
const result = await resource.read({
|
|
776
|
+
uri,
|
|
777
|
+
runtimeContext: createRuntimeContext(),
|
|
778
|
+
});
|
|
779
|
+
// Build content object based on what's provided (text or blob, not both)
|
|
780
|
+
const content: {
|
|
781
|
+
uri: string;
|
|
782
|
+
mimeType?: string;
|
|
783
|
+
text?: string;
|
|
784
|
+
blob?: string;
|
|
785
|
+
} = { uri: result.uri };
|
|
786
|
+
|
|
787
|
+
if (result.mimeType) {
|
|
788
|
+
content.mimeType = result.mimeType;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// MCP SDK expects either text or blob content, not both
|
|
792
|
+
if (result.text !== undefined) {
|
|
793
|
+
return {
|
|
794
|
+
contents: [
|
|
795
|
+
{
|
|
796
|
+
uri: result.uri,
|
|
797
|
+
mimeType: result.mimeType,
|
|
798
|
+
text: result.text,
|
|
799
|
+
},
|
|
800
|
+
],
|
|
801
|
+
};
|
|
802
|
+
} else if (result.blob !== undefined) {
|
|
803
|
+
return {
|
|
804
|
+
contents: [
|
|
805
|
+
{
|
|
806
|
+
uri: result.uri,
|
|
807
|
+
mimeType: result.mimeType,
|
|
808
|
+
blob: result.blob,
|
|
809
|
+
},
|
|
810
|
+
],
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// Fallback to empty text if neither provided
|
|
815
|
+
return {
|
|
816
|
+
contents: [
|
|
817
|
+
{ uri: result.uri, mimeType: result.mimeType, text: "" },
|
|
818
|
+
],
|
|
819
|
+
};
|
|
820
|
+
},
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
return { server, tools, prompts, resources };
|
|
641
825
|
};
|
|
642
826
|
|
|
643
827
|
const fetch = async (req: Request, env: TEnv) => {
|