servers: make tool call result spec compatible

This commit is contained in:
David Soria Parra 2024-12-02 13:12:15 +00:00
commit 788098220e
No known key found for this signature in database
5 changed files with 269 additions and 329 deletions

View file

@ -529,7 +529,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
case "fork_repository": {
const args = ForkRepositorySchema.parse(request.params.arguments);
const fork = await forkRepository(args.owner, args.repo, args.organization);
return { toolResult: fork };
return { content: [{ type: "text", text: JSON.stringify(fork, null, 2) }] };
}
case "create_branch": {
@ -562,25 +562,25 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
sha
});
return { toolResult: branch };
return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }] };
}
case "search_repositories": {
const args = SearchRepositoriesSchema.parse(request.params.arguments);
const results = await searchRepositories(args.query, args.page, args.perPage);
return { toolResult: results };
return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] };
}
case "create_repository": {
const args = CreateRepositorySchema.parse(request.params.arguments);
const repository = await createRepository(args);
return { toolResult: repository };
return { content: [{ type: "text", text: JSON.stringify(repository, null, 2) }] };
}
case "get_file_contents": {
const args = GetFileContentsSchema.parse(request.params.arguments);
const contents = await getFileContents(args.owner, args.repo, args.path, args.branch);
return { toolResult: contents };
return { content: [{ type: "text", text: JSON.stringify(contents, null, 2) }] };
}
case "create_or_update_file": {
@ -594,7 +594,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
args.branch,
args.sha
);
return { toolResult: result };
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
case "push_files": {
@ -606,21 +606,21 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
args.files,
args.message
);
return { toolResult: result };
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
case "create_issue": {
const args = CreateIssueSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const issue = await createIssue(owner, repo, options);
return { toolResult: issue };
return { content: [{ type: "text", text: JSON.stringify(issue, null, 2) }] };
}
case "create_pull_request": {
const args = CreatePullRequestSchema.parse(request.params.arguments);
const { owner, repo, ...options } = args;
const pullRequest = await createPullRequest(owner, repo, options);
return { toolResult: pullRequest };
return { content: [{ type: "text", text: JSON.stringify(pullRequest, null, 2) }] };
}
default:

View file

@ -437,7 +437,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
case "fork_repository": {
const args = ForkRepositorySchema.parse(request.params.arguments);
const fork = await forkProject(args.project_id, args.namespace);
return { toolResult: fork };
return { content: [{ type: "text", text: JSON.stringify(fork, null, 2) }] };
}
case "create_branch": {
@ -452,25 +452,25 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
ref
});
return { toolResult: branch };
return { content: [{ type: "text", text: JSON.stringify(branch, null, 2) }] };
}
case "search_repositories": {
const args = SearchRepositoriesSchema.parse(request.params.arguments);
const results = await searchProjects(args.search, args.page, args.per_page);
return { toolResult: results };
return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] };
}
case "create_repository": {
const args = CreateRepositorySchema.parse(request.params.arguments);
const repository = await createRepository(args);
return { toolResult: repository };
return { content: [{ type: "text", text: JSON.stringify(repository, null, 2) }] };
}
case "get_file_contents": {
const args = GetFileContentsSchema.parse(request.params.arguments);
const contents = await getFileContents(args.project_id, args.file_path, args.ref);
return { toolResult: contents };
return { content: [{ type: "text", text: JSON.stringify(contents, null, 2) }] };
}
case "create_or_update_file": {
@ -483,7 +483,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
args.branch,
args.previous_path
);
return { toolResult: result };
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
case "push_files": {
@ -494,21 +494,21 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
args.branch,
args.files.map(f => ({ path: f.file_path, content: f.content }))
);
return { toolResult: result };
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
}
case "create_issue": {
const args = CreateIssueSchema.parse(request.params.arguments);
const { project_id, ...options } = args;
const issue = await createIssue(project_id, options);
return { toolResult: issue };
return { content: [{ type: "text", text: JSON.stringify(issue, null, 2) }] };
}
case "create_merge_request": {
const args = CreateMergeRequestSchema.parse(request.params.arguments);
const { project_id, ...options } = args;
const mergeRequest = await createMergeRequest(project_id, options);
return { toolResult: mergeRequest };
return { content: [{ type: "text", text: JSON.stringify(mergeRequest, null, 2) }] };
}
default:

View file

@ -315,18 +315,15 @@ async function handleGeocode(address: string) {
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Geocoding failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -336,7 +333,6 @@ async function handleGeocode(address: string) {
}, null, 2)
}],
isError: false
}
};
}
@ -350,18 +346,15 @@ async function handleReverseGeocode(latitude: number, longitude: number) {
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Reverse geocoding failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -371,7 +364,6 @@ async function handleReverseGeocode(latitude: number, longitude: number) {
}, null, 2)
}],
isError: false
}
};
}
@ -396,18 +388,15 @@ async function handlePlaceSearch(
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Place search failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -422,7 +411,6 @@ async function handlePlaceSearch(
}, null, 2)
}],
isError: false
}
};
}
@ -436,18 +424,15 @@ async function handlePlaceDetails(place_id: string) {
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Place details request failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -462,7 +447,6 @@ async function handlePlaceDetails(place_id: string) {
}, null, 2)
}],
isError: false
}
};
}
async function handleDistanceMatrix(
@ -481,18 +465,15 @@ async function handleDistanceMatrix(
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Distance matrix request failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -508,7 +489,6 @@ async function handleDistanceMatrix(
}, null, 2)
}],
isError: false
}
};
}
@ -525,18 +505,15 @@ async function handleElevation(locations: Array<{ latitude: number; longitude: n
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Elevation request failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -548,7 +525,6 @@ async function handleElevation(locations: Array<{ latitude: number; longitude: n
}, null, 2)
}],
isError: false
}
};
}
@ -568,18 +544,15 @@ async function handleDirections(
if (data.status !== "OK") {
return {
toolResult: {
content: [{
type: "text",
text: `Directions request failed: ${data.error_message || data.status}`
}],
isError: true
}
};
}
return {
toolResult: {
content: [{
type: "text",
text: JSON.stringify({
@ -597,7 +570,6 @@ async function handleDirections(
}, null, 2)
}],
isError: false
}
};
}
@ -676,24 +648,20 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
default:
return {
toolResult: {
content: [{
type: "text",
text: `Unknown tool: ${request.params.name}`
}],
isError: true
}
};
}
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
}
};
}
});

View file

@ -377,26 +377,26 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
switch (name) {
case "create_entities":
return { toolResult: await knowledgeGraphManager.createEntities(args.entities as Entity[]) };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.createEntities(args.entities as Entity[]), null, 2) }] };
case "create_relations":
return { toolResult: await knowledgeGraphManager.createRelations(args.relations as Relation[]) };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.createRelations(args.relations as Relation[]), null, 2) }] };
case "add_observations":
return { toolResult: await knowledgeGraphManager.addObservations(args.observations as { entityName: string; contents: string[] }[]) };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.addObservations(args.observations as { entityName: string; contents: string[] }[]), null, 2) }] };
case "delete_entities":
await knowledgeGraphManager.deleteEntities(args.entityNames as string[]);
return { toolResult: "Entities deleted successfully" };
return { content: [{ type: "text", text: "Entities deleted successfully" }] };
case "delete_observations":
await knowledgeGraphManager.deleteObservations(args.deletions as { entityName: string; observations: string[] }[]);
return { toolResult: "Observations deleted successfully" };
return { content: [{ type: "text", text: "Observations deleted successfully" }] };
case "delete_relations":
await knowledgeGraphManager.deleteRelations(args.relations as Relation[]);
return { toolResult: "Relations deleted successfully" };
return { content: [{ type: "text", text: "Relations deleted successfully" }] };
case "read_graph":
return { toolResult: await knowledgeGraphManager.readGraph() };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.readGraph(), null, 2) }] };
case "search_nodes":
return { toolResult: await knowledgeGraphManager.searchNodes(args.query as string) };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.searchNodes(args.query as string), null, 2) }] };
case "open_nodes":
return { toolResult: await knowledgeGraphManager.openNodes(args.names as string[]) };
return { content: [{ type: "text", text: JSON.stringify(await knowledgeGraphManager.openNodes(args.names as string[]), null, 2) }] };
default:
throw new Error(`Unknown tool: ${name}`);
}

View file

@ -124,20 +124,18 @@ async function ensureBrowser() {
return page!;
}
async function handleToolCall(name: string, args: any): Promise<{ toolResult: CallToolResult }> {
async function handleToolCall(name: string, args: any): Promise<CallToolResult> {
const page = await ensureBrowser();
switch (name) {
case "puppeteer_navigate":
await page.goto(args.url);
return {
toolResult: {
content: [{
type: "text",
text: `Navigated to ${args.url}`,
}],
isError: false,
},
};
case "puppeteer_screenshot": {
@ -151,13 +149,11 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
if (!screenshot) {
return {
toolResult: {
content: [{
type: "text",
text: args.selector ? `Element not found: ${args.selector}` : "Screenshot failed",
}],
isError: true,
},
};
}
@ -167,7 +163,6 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
});
return {
toolResult: {
content: [
{
type: "text",
@ -180,7 +175,6 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
} as ImageContent,
],
isError: false,
},
};
}
@ -188,23 +182,19 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
try {
await page.click(args.selector);
return {
toolResult: {
content: [{
type: "text",
text: `Clicked: ${args.selector}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to click ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}
@ -213,23 +203,19 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
await page.waitForSelector(args.selector);
await page.type(args.selector, args.value);
return {
toolResult: {
content: [{
type: "text",
text: `Filled ${args.selector} with: ${args.value}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to fill ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}
@ -238,23 +224,19 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
await page.waitForSelector(args.selector);
await page.select(args.selector, args.value);
return {
toolResult: {
content: [{
type: "text",
text: `Selected ${args.selector} with: ${args.value}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to select ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}
@ -263,23 +245,19 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
await page.waitForSelector(args.selector);
await page.hover(args.selector);
return {
toolResult: {
content: [{
type: "text",
text: `Hovered ${args.selector}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to hover ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}
@ -307,7 +285,6 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
}, args.script);
return {
toolResult: {
content: [
{
type: "text",
@ -315,29 +292,24 @@ async function handleToolCall(name: string, args: any): Promise<{ toolResult: Ca
},
],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Script execution failed: ${(error as Error).message}`,
}],
isError: true,
},
};
}
default:
return {
toolResult: {
content: [{
type: "text",
text: `Unknown tool: ${name}`,
}],
isError: true,
},
};
}
}