@revojs/ai 0.2.20 → 0.2.21

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/dist/index.d.ts CHANGED
@@ -50,6 +50,9 @@ export interface ToolDefinition {
50
50
  export interface ToolExecutionOptions {
51
51
  signal?: AbortSignal;
52
52
  }
53
+ export interface ModelContextProtocolServerOptions {
54
+ filter?: (tool: ToolDescription) => boolean;
55
+ }
53
56
  export interface ToolDescription extends LanguageModelFunctionTool {
54
57
  inputSchema: JSONObject;
55
58
  annotations?: ToolAnnotations;
@@ -683,7 +686,7 @@ export declare function useTools(scope: Scope): {
683
686
  callTool: (name: string, input?: JSONObject, options?: ToolExecutionOptions) => Promise<CallToolResult>;
684
687
  executeTool(call: LanguageModelToolCall, options?: ToolExecutionOptions): Promise<LanguageModelToolResultPart>;
685
688
  };
686
- export declare function handleModelContextProtocol(scope: Scope): Promise<Response>;
689
+ export declare function handleModelContextProtocol(scope: Scope, options?: ModelContextProtocolServerOptions): Promise<Response>;
687
690
  export declare function useModelContextProtocol(options: ModelContextProtocolConnectOptions): Promise<ModelContextProtocolConnection>;
688
691
  export declare function defineProvider<TProvider extends Provider>(provider: TProvider): TProvider;
689
692
  export declare function useAi<TSource extends Source<Provider> = typeof import("#providers").default>(scope: Scope): AiConnections<TSource>;
package/dist/index.js CHANGED
@@ -206,7 +206,7 @@ function useTools(scope) {
206
206
  }
207
207
  };
208
208
  }
209
- async function handleModelContextProtocol(scope) {
209
+ async function handleModelContextProtocol(scope, options = {}) {
210
210
  requireSameOrigin(scope);
211
211
  const { request } = useServer(scope);
212
212
  if (request.method !== "POST") return new Response(null, {
@@ -223,13 +223,18 @@ async function handleModelContextProtocol(scope) {
223
223
  jsonSchemaValidator: new CfWorkerJsonSchemaValidator()
224
224
  });
225
225
  const transport = new WebStandardStreamableHTTPServerTransport({ enableJsonResponse: true });
226
- server.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: (await collection.listTools()).map((tool) => ({
226
+ const listTools = async () => {
227
+ const tools = await collection.listTools();
228
+ return options.filter ? tools.filter(options.filter) : tools;
229
+ };
230
+ server.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: (await listTools()).map((tool) => ({
227
231
  name: tool.name,
228
232
  description: tool.description,
229
233
  inputSchema: tool.inputSchema,
230
234
  annotations: tool.annotations
231
235
  })) }));
232
236
  server.server.setRequestHandler(CallToolRequestSchema, async ({ params }, extra) => {
237
+ if (options.filter && !(await listTools()).some((tool) => tool.name === params.name)) throw new McpError(ErrorCode.InvalidParams, `Unknown tool "${params.name}".`);
233
238
  try {
234
239
  return await collection.callTool(params.name, params.arguments ?? {}, { signal: AbortSignal.any([request.signal, extra.signal]) });
235
240
  } catch (error) {
@@ -1,5 +1,4 @@
1
- import { handleModelContextProtocol } from "../../..";
2
1
  declare const _default: {
3
- fetch: typeof handleModelContextProtocol;
2
+ fetch: (scope: import("revojs").Scope) => Promise<Response>;
4
3
  };
5
4
  export default _default;
@@ -1,6 +1,6 @@
1
1
  import { handleModelContextProtocol } from "../../../index.js";
2
2
  import { defineRoute } from "@revojs/server";
3
3
  //#region src/routes/v1/model-context-protocol/delete.ts
4
- var delete_default = defineRoute({ fetch: handleModelContextProtocol });
4
+ var delete_default = defineRoute({ fetch: (scope) => handleModelContextProtocol(scope) });
5
5
  //#endregion
6
6
  export { delete_default as default };
@@ -1,5 +1,4 @@
1
- import { handleModelContextProtocol } from "../../..";
2
1
  declare const _default: {
3
- fetch: typeof handleModelContextProtocol;
2
+ fetch: (scope: import("revojs").Scope) => Promise<Response>;
4
3
  };
5
4
  export default _default;
@@ -1,6 +1,6 @@
1
1
  import { handleModelContextProtocol } from "../../../index.js";
2
2
  import { defineRoute } from "@revojs/server";
3
3
  //#region src/routes/v1/model-context-protocol/get.ts
4
- var get_default = defineRoute({ fetch: handleModelContextProtocol });
4
+ var get_default = defineRoute({ fetch: (scope) => handleModelContextProtocol(scope) });
5
5
  //#endregion
6
6
  export { get_default as default };
@@ -1,5 +1,4 @@
1
- import { handleModelContextProtocol } from "../../..";
2
1
  declare const _default: {
3
- fetch: typeof handleModelContextProtocol;
2
+ fetch: (scope: import("revojs").Scope) => Promise<Response>;
4
3
  };
5
4
  export default _default;
@@ -1,6 +1,6 @@
1
1
  import { handleModelContextProtocol } from "../../../index.js";
2
2
  import { defineRoute } from "@revojs/server";
3
3
  //#region src/routes/v1/model-context-protocol/post.ts
4
- var post_default = defineRoute({ fetch: handleModelContextProtocol });
4
+ var post_default = defineRoute({ fetch: (scope) => handleModelContextProtocol(scope) });
5
5
  //#endregion
6
6
  export { post_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revojs/ai",
3
- "version": "0.2.20",
3
+ "version": "0.2.21",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",