tool call logging, centralized temp dir
This commit is contained in:
+15
-48
@@ -1,9 +1,8 @@
|
||||
import { appendFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { cached } from "@ark/util";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
||||
import type { FastMCP, Tool } from "fastmcp";
|
||||
import { log } from "../utils/cli.ts";
|
||||
import { parseRepoContext, type RepoContext } from "../utils/github.ts";
|
||||
|
||||
export interface ToolResult {
|
||||
@@ -32,49 +31,6 @@ export interface McpContext extends RepoContext {
|
||||
octokit: Octokit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the log file path
|
||||
*/
|
||||
function getLogPath(): string {
|
||||
return join(process.cwd(), "log.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* Log MCP tool call information to log.txt
|
||||
*/
|
||||
function logToolCall({
|
||||
toolName,
|
||||
request,
|
||||
error,
|
||||
success,
|
||||
}: {
|
||||
toolName: string;
|
||||
request: unknown;
|
||||
error?: unknown;
|
||||
success?: boolean;
|
||||
}) {
|
||||
const logPath = getLogPath();
|
||||
const timestamp = new Date().toISOString();
|
||||
const requestStr = JSON.stringify(request, null, 2);
|
||||
|
||||
let logEntry = `[${timestamp}] Tool: ${toolName}\nRequest: ${requestStr}\n`;
|
||||
|
||||
if (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
const errorStack = error instanceof Error ? error.stack : undefined;
|
||||
logEntry += `Error: ${errorMessage}\n`;
|
||||
if (errorStack) {
|
||||
logEntry += `Stack: ${errorStack}\n`;
|
||||
}
|
||||
logEntry += `Status: FAILED\n`;
|
||||
} else if (success !== undefined) {
|
||||
logEntry += `Status: ${success ? "SUCCESS" : "FAILED"}\n`;
|
||||
}
|
||||
|
||||
logEntry += `${"=".repeat(80)}\n\n`;
|
||||
appendFileSync(logPath, logEntry, "utf-8");
|
||||
}
|
||||
|
||||
export const tool = <const params>(toolDef: Tool<any, StandardSchemaV1<params>>) => {
|
||||
// Wrap the execute function to add logging with the tool name
|
||||
const toolName = toolDef.name;
|
||||
@@ -82,15 +38,26 @@ export const tool = <const params>(toolDef: Tool<any, StandardSchemaV1<params>>)
|
||||
|
||||
toolDef.execute = async (args: params, context: any) => {
|
||||
try {
|
||||
logToolCall({ toolName, request: args });
|
||||
const result = await originalExecute(args, context);
|
||||
// Check if result is a ToolResult with isError property
|
||||
const isError =
|
||||
result && typeof result === "object" && "isError" in result && result.isError === true;
|
||||
logToolCall({ toolName, request: args, success: !isError });
|
||||
const resultData =
|
||||
result && typeof result === "object" && "content" in result
|
||||
? (result as ToolResult).content[0]?.text
|
||||
: undefined;
|
||||
|
||||
if (isError && resultData) {
|
||||
log.toolCall({ toolName, request: args, error: resultData });
|
||||
} else if (resultData) {
|
||||
log.toolCall({ toolName, request: args, result: resultData });
|
||||
} else {
|
||||
log.toolCall({ toolName, request: args });
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
logToolCall({ toolName, request: args, error });
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
log.toolCall({ toolName, request: args, error: errorMessage });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user