-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathresource.ts
More file actions
71 lines (65 loc) · 2.14 KB
/
resource.ts
File metadata and controls
71 lines (65 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import { McpContext } from "./types";
export interface ServerResource {
mcp: {
uri: string;
name: string;
description?: string;
title?: string;
_meta?: {
/** Set this on a resource if it *always* requires a signed-in user to work. */
requiresAuth?: boolean;
/** Set this on a resource if it uses Gemini in Firebase API in any way. */
requiresGemini?: boolean;
};
};
fn: (uri: string, ctx: McpContext) => Promise<ReadResourceResult>;
}
export function resource(
options: ServerResource["mcp"],
fnOrText: ServerResource["fn"] | string,
): ServerResource {
const fn: ServerResource["fn"] =
typeof fnOrText === "string"
? async (uri) => ({ contents: [{ uri, text: fnOrText }] })
: fnOrText;
return { mcp: options, fn };
}
export interface ServerResourceTemplate {
mcp: {
uriTemplate: string;
/** How to know if a URI matches this template, can be a string (prefix), regex, or function. */
name: string;
description?: string;
title?: string;
_meta?: {
/** Set this on a resource if it *always* requires a signed-in user to work. */
requiresAuth?: boolean;
/** Set this on a resource if it uses Gemini in Firebase API in any way. */
requiresGemini?: boolean;
};
};
match: (uri: string) => boolean;
fn: (uri: string, ctx: McpContext) => Promise<ReadResourceResult>;
}
export function resourceTemplate(
options: ServerResourceTemplate["mcp"] & {
match: string | RegExp | ServerResourceTemplate["match"];
},
fnOrText: ServerResourceTemplate["fn"] | string,
): ServerResourceTemplate {
let matchFn: ServerResourceTemplate["match"];
const { match, ...mcp } = options;
if (match instanceof RegExp) {
matchFn = (uri) => match.test(uri);
} else if (typeof match === "string") {
matchFn = (uri) => uri.startsWith(match);
} else {
matchFn = match;
}
const fn: ServerResourceTemplate["fn"] =
typeof fnOrText === "string"
? async (uri) => ({ contents: [{ uri, text: fnOrText }] })
: fnOrText;
return { mcp, match: matchFn, fn };
}