Added general meta data section to add pairs for all requests
This commit is contained in:
parent
78c56b350b
commit
842aa3c8ca
4 changed files with 835 additions and 25 deletions
|
|
@ -12,9 +12,14 @@ type JsonValue =
|
|||
| { [key: string]: JsonValue };
|
||||
|
||||
// List available prompts
|
||||
export async function listPrompts(client: Client): Promise<McpResponse> {
|
||||
export async function listPrompts(
|
||||
client: Client,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const response = await client.listPrompts();
|
||||
const params =
|
||||
metaData && Object.keys(metaData).length > 0 ? { _meta: metaData } : {};
|
||||
const response = await client.listPrompts(params);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
|
|
@ -28,6 +33,7 @@ export async function getPrompt(
|
|||
client: Client,
|
||||
name: string,
|
||||
args?: Record<string, JsonValue>,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
// Convert all arguments to strings for prompt arguments
|
||||
|
|
@ -44,10 +50,16 @@ export async function getPrompt(
|
|||
}
|
||||
}
|
||||
|
||||
const response = await client.getPrompt({
|
||||
const params: any = {
|
||||
name,
|
||||
arguments: stringArgs,
|
||||
});
|
||||
};
|
||||
|
||||
if (metaData && Object.keys(metaData).length > 0) {
|
||||
params._meta = metaData;
|
||||
}
|
||||
|
||||
const response = await client.getPrompt(params);
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,14 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|||
import { McpResponse } from "./types.js";
|
||||
|
||||
// List available resources
|
||||
export async function listResources(client: Client): Promise<McpResponse> {
|
||||
export async function listResources(
|
||||
client: Client,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const response = await client.listResources();
|
||||
const params =
|
||||
metaData && Object.keys(metaData).length > 0 ? { _meta: metaData } : {};
|
||||
const response = await client.listResources(params);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
|
|
@ -17,9 +22,14 @@ export async function listResources(client: Client): Promise<McpResponse> {
|
|||
export async function readResource(
|
||||
client: Client,
|
||||
uri: string,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const response = await client.readResource({ uri });
|
||||
const params: any = { uri };
|
||||
if (metaData && Object.keys(metaData).length > 0) {
|
||||
params._meta = metaData;
|
||||
}
|
||||
const response = await client.readResource(params);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
|
|
@ -31,9 +41,12 @@ export async function readResource(
|
|||
// List resource templates
|
||||
export async function listResourceTemplates(
|
||||
client: Client,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const response = await client.listResourceTemplates();
|
||||
const params =
|
||||
metaData && Object.keys(metaData).length > 0 ? { _meta: metaData } : {};
|
||||
const response = await client.listResourceTemplates(params);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
|
|
|
|||
|
|
@ -19,9 +19,14 @@ type JsonSchemaType = {
|
|||
items?: JsonSchemaType;
|
||||
};
|
||||
|
||||
export async function listTools(client: Client): Promise<McpResponse> {
|
||||
export async function listTools(
|
||||
client: Client,
|
||||
metaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const response = await client.listTools();
|
||||
const params =
|
||||
metaData && Object.keys(metaData).length > 0 ? { _meta: metaData } : {};
|
||||
const response = await client.listTools(params);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
|
|
@ -82,9 +87,11 @@ export async function callTool(
|
|||
client: Client,
|
||||
name: string,
|
||||
args: Record<string, JsonValue>,
|
||||
generalMetaData?: Record<string, string>,
|
||||
toolSpecificMetaData?: Record<string, string>,
|
||||
): Promise<McpResponse> {
|
||||
try {
|
||||
const toolsResponse = await listTools(client);
|
||||
const toolsResponse = await listTools(client, generalMetaData);
|
||||
const tools = toolsResponse.tools as Tool[];
|
||||
const tool = tools.find((t) => t.name === name);
|
||||
|
||||
|
|
@ -106,9 +113,23 @@ export async function callTool(
|
|||
}
|
||||
}
|
||||
|
||||
// Merge general metadata with tool-specific metadata
|
||||
// Tool-specific metadata takes precedence over general metadata
|
||||
let mergedMeta: Record<string, string> | undefined;
|
||||
if (generalMetaData || toolSpecificMetaData) {
|
||||
mergedMeta = {
|
||||
...(generalMetaData || {}),
|
||||
...(toolSpecificMetaData || {}),
|
||||
};
|
||||
}
|
||||
|
||||
const response = await client.callTool({
|
||||
name: name,
|
||||
arguments: convertedArgs,
|
||||
_meta:
|
||||
mergedMeta && Object.keys(mergedMeta).length > 0
|
||||
? mergedMeta
|
||||
: undefined,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -42,8 +42,10 @@ type Args = {
|
|||
logLevel?: LogLevel;
|
||||
toolName?: string;
|
||||
toolArg?: Record<string, JsonValue>;
|
||||
toolMeta?: Record<string, string>;
|
||||
transport?: "sse" | "stdio" | "http";
|
||||
headers?: Record<string, string>;
|
||||
metaData?: Record<string, string>;
|
||||
};
|
||||
|
||||
function createTransportOptions(
|
||||
|
|
@ -121,7 +123,7 @@ async function callMethod(args: Args): Promise<void> {
|
|||
|
||||
// Tools methods
|
||||
if (args.method === "tools/list") {
|
||||
result = await listTools(client);
|
||||
result = await listTools(client, args.metaData);
|
||||
} else if (args.method === "tools/call") {
|
||||
if (!args.toolName) {
|
||||
throw new Error(
|
||||
|
|
@ -129,11 +131,17 @@ async function callMethod(args: Args): Promise<void> {
|
|||
);
|
||||
}
|
||||
|
||||
result = await callTool(client, args.toolName, args.toolArg || {});
|
||||
result = await callTool(
|
||||
client,
|
||||
args.toolName,
|
||||
args.toolArg || {},
|
||||
args.metaData,
|
||||
args.toolMeta,
|
||||
);
|
||||
}
|
||||
// Resources methods
|
||||
else if (args.method === "resources/list") {
|
||||
result = await listResources(client);
|
||||
result = await listResources(client, args.metaData);
|
||||
} else if (args.method === "resources/read") {
|
||||
if (!args.uri) {
|
||||
throw new Error(
|
||||
|
|
@ -141,13 +149,13 @@ async function callMethod(args: Args): Promise<void> {
|
|||
);
|
||||
}
|
||||
|
||||
result = await readResource(client, args.uri);
|
||||
result = await readResource(client, args.uri, args.metaData);
|
||||
} else if (args.method === "resources/templates/list") {
|
||||
result = await listResourceTemplates(client);
|
||||
result = await listResourceTemplates(client, args.metaData);
|
||||
}
|
||||
// Prompts methods
|
||||
else if (args.method === "prompts/list") {
|
||||
result = await listPrompts(client);
|
||||
result = await listPrompts(client, args.metaData);
|
||||
} else if (args.method === "prompts/get") {
|
||||
if (!args.promptName) {
|
||||
throw new Error(
|
||||
|
|
@ -155,7 +163,12 @@ async function callMethod(args: Args): Promise<void> {
|
|||
);
|
||||
}
|
||||
|
||||
result = await getPrompt(client, args.promptName, args.promptArgs || {});
|
||||
result = await getPrompt(
|
||||
client,
|
||||
args.promptName,
|
||||
args.promptArgs || {},
|
||||
args.metaData,
|
||||
);
|
||||
}
|
||||
// Logging methods
|
||||
else if (args.method === "logging/setLevel") {
|
||||
|
|
@ -327,6 +340,8 @@ function parseArgs(): Args {
|
|||
|
||||
const options = program.opts() as Omit<Args, "target"> & {
|
||||
header?: Record<string, string>;
|
||||
meta?: Record<string, JsonValue>;
|
||||
toolMeta?: Record<string, JsonValue>;
|
||||
};
|
||||
|
||||
let remainingArgs = program.args;
|
||||
|
|
@ -344,6 +359,22 @@ function parseArgs(): Args {
|
|||
target: finalArgs,
|
||||
...options,
|
||||
headers: options.header, // commander.js uses 'header' field, map to 'headers'
|
||||
metaData: options.meta
|
||||
? Object.fromEntries(
|
||||
Object.entries(options.meta).map(([key, value]) => [
|
||||
key,
|
||||
String(value),
|
||||
]),
|
||||
)
|
||||
: undefined,
|
||||
toolMeta: options.toolMeta
|
||||
? Object.fromEntries(
|
||||
Object.entries(options.toolMeta).map(([key, value]) => [
|
||||
key,
|
||||
String(value),
|
||||
]),
|
||||
)
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue