Skip to content

Commit 69905fd

Browse files
chore: minor changes
1 parent 0073154 commit 69905fd

File tree

4 files changed

+98
-50
lines changed

4 files changed

+98
-50
lines changed

examples/mcp-sdk/src/index.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { Hono } from "hono";
33
import {
44
muppet,
55
describeTool,
6-
// staticResource,
7-
// dynamicResource,
86
describePrompt,
97
mValidator,
8+
registerResources,
109
} from "muppet";
1110
import z from "zod";
1211
import pino from "pino";
@@ -38,35 +37,18 @@ app.post(
3837
},
3938
);
4039

41-
// Define static resources
42-
// app.post(
43-
// "/static/*",
44-
// staticResource({
45-
// name: "Static Resource",
46-
// description: "A static resource",
47-
// resource: {
48-
// path: "E:/dev/muppet/muppet/examples/mcp-sdk/dist/static",
49-
// },
50-
// }),
51-
// );
52-
53-
// // Define Dynamic resources
54-
// app.post(
55-
// "/dynamic/*",
56-
// dynamicResource({
57-
// name: "Dynamic Resource",
58-
// description: "A dynamic resource",
59-
// }),
60-
// (c) => {
61-
// return c.json([
62-
// {
63-
// uri: "file:///logs/app.log",
64-
// name: "Application Logs",
65-
// mimeType: "text/plain",
66-
// },
67-
// ]);
68-
// },
69-
// );
40+
app.post(
41+
"/documents",
42+
registerResources((c) => {
43+
return c.json([
44+
{
45+
uri: "https://lorem.ipsum",
46+
name: "Todo list",
47+
mimeType: "text/plain",
48+
},
49+
]);
50+
}),
51+
);
7052

7153
// Define a simple prompt
7254
app.post(
@@ -102,4 +84,30 @@ muppet(app, {
10284
"/Users/adityamathur/dev/muppet-dev/muppet/examples/mcp-sdk/dist/main.log",
10385
),
10486
},
87+
resources: {
88+
https: () => {
89+
return [
90+
{
91+
uri: "task1",
92+
text: "Task 1",
93+
},
94+
{
95+
uri: "task2",
96+
text: "Task 2",
97+
},
98+
{
99+
uri: "task3",
100+
text: "Task 3",
101+
},
102+
{
103+
uri: "task4",
104+
text: "Task 4",
105+
},
106+
{
107+
uri: "task5",
108+
text: "Task 5",
109+
},
110+
];
111+
},
112+
},
105113
});

packages/core/src/muppet.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
DescribeOptions,
2020
MuppetConfiguration,
2121
PromptConfiguration,
22+
Resource,
2223
ServerConfiguration,
2324
ToolHandlerResponse,
2425
} from "./types";
@@ -407,20 +408,40 @@ function createResourceApp<
407408
});
408409

409410
app.post("/list", async (c) => {
410-
const resources = await Promise.all(
411+
const responses = await Promise.all(
411412
Object.values(config.resources ?? {}).map(async ({ path }) => {
412413
const res = await hono.request(path, {
413414
method: "POST",
414415
headers: c.req.header(),
415416
});
416417

417-
return res.json();
418+
return res.json() as Promise<Resource[]>;
418419
}),
419420
);
420421

422+
const resources = responses.flat(2).map((resource: Resource) => {
423+
const schema = {
424+
name: resource.name,
425+
description: resource.description,
426+
mimeType: resource.mimeType,
427+
};
428+
429+
if (resource.type === "template") {
430+
return {
431+
...schema,
432+
uriTemplate: resource.uri,
433+
};
434+
}
435+
436+
return {
437+
...schema,
438+
uri: resource.uri,
439+
};
440+
});
441+
421442
return c.json({
422443
result: {
423-
resources: resources.flat(),
444+
resources,
424445
},
425446
});
426447
});

packages/core/src/resources.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { Env, Handler, Input } from "hono";
22
import type { BlankEnv, BlankInput, TypedResponse } from "hono/types";
33
import { McpPrimitives, uniqueSymbol } from "./utils";
4+
import type { Resource } from "./types";
45

56
export function registerResources<
67
E extends Env = BlankEnv,
78
P extends string = string,
89
I extends Input = BlankInput,
910
>(
10-
handler: Handler<E, P, I, TypedResponse<"json", 200>>,
11-
): Handler<E, P, I, TypedResponse<"json", 200>> {
11+
handler: Handler<E, P, I, TypedResponse<Resource[], 200>>,
12+
): Handler<E, P, I, TypedResponse<Resource[], 200>> {
1213
return Object.assign(handler, {
1314
[uniqueSymbol]: {
1415
type: McpPrimitives.RESOURCES,

packages/core/src/types.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,35 @@ export type ResourceConfiguration = Record<
4444
}
4545
>;
4646

47+
export type Resource =
48+
| {
49+
type?: "direct";
50+
uri: string;
51+
name: string;
52+
description?: string;
53+
mimeType?: string;
54+
}
55+
| {
56+
type: "template";
57+
uri: string;
58+
name: string;
59+
description?: string;
60+
mimeType?: string;
61+
};
62+
63+
export type ResourceResponse = {
64+
uri: string;
65+
mimeType?: string;
66+
/**
67+
* For text resources
68+
*/
69+
text?: string;
70+
/**
71+
* For binary resources (base64 encoded)
72+
*/
73+
blob?: string;
74+
};
75+
4776
export type ConceptConfiguration = Record<
4877
string,
4978
| (DescribeOptions & {
@@ -72,19 +101,8 @@ export type MuppetConfiguration = {
72101
events?: Emitter<AvailableEvents>;
73102
resources?: Record<
74103
string,
75-
(uri: string) => Promisify<
76-
{
77-
uri: string;
78-
mimeType: string;
79-
/**
80-
* For text resources
81-
*/
82-
text?: string;
83-
/**
84-
* For binary resources (base64 encoded)
85-
*/
86-
blob?: string;
87-
}[]
88-
>
104+
(
105+
uri: string,
106+
) => Promisify<ResourceResponse[] | { contents: ResourceResponse[] }>
89107
>;
90108
};

0 commit comments

Comments
 (0)