genkitx-mcp
Version:
A Genkit plugin that provides interoperability between Genkit and Model Context Protocol (MCP). Both client and server use cases are supported.
71 lines • 2.32 kB
JavaScript
import { z } from "genkit";
async function registerResourceTools(ai, client, params) {
ai.defineTool(
{
name: `${params.name}/list_resources`,
description: `list all available resources for '${params.name}'`,
inputSchema: z.object({
/** Provide a cursor for accessing additional paginated results. */
cursor: z.string().optional(),
/** When specified, automatically paginate and fetch all resources. */
all: z.boolean().optional()
})
},
async ({ cursor, all }) => {
if (!all) {
return client.listResources();
}
let currentCursor = cursor;
const resources = [];
while (true) {
const { nextCursor, resources: newResources } = await client.listResources({ cursor: currentCursor });
resources.push(...newResources);
currentCursor = nextCursor;
if (!currentCursor) break;
}
return { resources };
}
);
ai.defineTool(
{
name: `${params.name}/list_resource_templates`,
description: `list all available resource templates for '${params.name}'`,
inputSchema: z.object({
/** Provide a cursor for accessing additional paginated results. */
cursor: z.string().optional(),
/** When specified, automatically paginate and fetch all resources. */
all: z.boolean().optional()
})
},
async ({ cursor, all }) => {
if (!all) {
return client.listResourceTemplates();
}
let currentCursor = cursor;
const resourceTemplates = [];
while (true) {
const { nextCursor, resourceTemplates: newResourceTemplates } = await client.listResourceTemplates({ cursor: currentCursor });
resourceTemplates.push(...newResourceTemplates);
currentCursor = nextCursor;
if (!currentCursor) break;
}
return { resourceTemplates };
}
);
ai.defineTool(
{
name: `${params.name}/read_resource`,
description: `this tool can read resources from '${params.name}'`,
inputSchema: z.object({
uri: z.string().describe("the URI of the resource to retrieve")
})
},
async ({ uri }) => {
return client.readResource({ uri });
}
);
}
export {
registerResourceTools
};
//# sourceMappingURL=resources.mjs.map