-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtool.ts
More file actions
67 lines (59 loc) · 2.33 KB
/
tool.ts
File metadata and controls
67 lines (59 loc) · 2.33 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
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { z, ZodTypeAny } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { McpContext, ServerFeature } from "./types";
import { cleanSchema } from "./util";
import { getDefaultFeatureAvailabilityCheck } from "./util/availability";
export type ServerToolMeta = {
/** Set this on a tool if it cannot work without a Firebase project directory. */
optionalProjectDir?: boolean;
/** Set this on a tool if it *always* requires a project to work. */
requiresProject?: boolean;
/** Set this on a tool if it *always* requires a signed-in user to work. */
requiresAuth?: boolean;
/** Tools are grouped by feature. --only can configure what tools is available. */
feature?: string;
};
export interface ServerTool<InputSchema extends ZodTypeAny = ZodTypeAny> {
mcp: {
name: string;
description?: string;
inputSchema: any;
annotations?: {
title?: string;
// If this tool modifies data or not.
readOnlyHint?: boolean;
// this tool can destroy data.
destructiveHint?: boolean;
// this tool is safe to run multiple times.
idempotentHint?: boolean;
// If this is true, it connects to the internet or other open world
// systems. If false, the tool only performs actions in an enclosed
// system, such as your project.
openWorldHint?: boolean;
};
_meta?: ServerToolMeta;
};
fn: (input: z.infer<InputSchema>, ctx: McpContext) => Promise<CallToolResult>;
isAvailable: (ctx: McpContext) => Promise<boolean>;
}
export function tool<InputSchema extends ZodTypeAny>(
feature: ServerFeature,
options: Omit<ServerTool<InputSchema>["mcp"], "inputSchema"> & {
inputSchema: InputSchema;
isAvailable?: (ctx: McpContext) => Promise<boolean>;
},
fn: ServerTool<InputSchema>["fn"],
): ServerTool {
const { isAvailable, ...mcpOptions } = options;
return {
mcp: { ...mcpOptions, inputSchema: cleanSchema(zodToJsonSchema(options.inputSchema)) },
fn,
isAvailable: (ctx: McpContext) => {
// default to the feature level availability check, but allow override
// We resolve this at runtime to allow for easier testing/mocking
const isAvailableFunc = isAvailable || getDefaultFeatureAvailabilityCheck(feature);
return isAvailableFunc(ctx);
},
};
}