-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.ts
More file actions
101 lines (92 loc) · 2.75 KB
/
tools.ts
File metadata and controls
101 lines (92 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import z from "zod";
// ============================================================================
// CONFIGURATION
// ============================================================================
const API_URL = process.env.API_ENDPOINT || "http://localhost:3001";
const _API_KEY = process.env.API_KEY || "";
// ============================================================================
// UTILITY FUNCTIONS
// ============================================================================
// Helper function for making API requests (supports GET, POST, body, etc.)
const _makeRequest = async <T>(
api_url = API_URL,
{
method = "GET",
headers = {},
body = undefined,
}: {
method?: string;
headers?: HeadersInit;
body?: unknown;
} = {},
): Promise<T | null> => {
try {
const fetchOptions: RequestInit = {
method,
headers,
};
if (body !== undefined) {
fetchOptions.body =
typeof body === "string" ? body : JSON.stringify(body);
// Set JSON header if not already set
if (
typeof headers === "object" &&
!(headers as Record<string, string>)["Content-Type"]
) {
(fetchOptions.headers as Record<string, string>)["Content-Type"] =
"application/json";
}
}
const response = await fetch(api_url, fetchOptions);
if (!response.ok) {
console.error(`Error: ${response.status} ${response.statusText}`);
return null;
}
const data = await response.json();
return data as T;
} catch (error) {
console.error("Fetch error:", error);
return null;
}
};
// ============================================================================
// TOOL DEFINITIONS
// ============================================================================
export const populateTools = (server: McpServer) => {
// Demo tool: Say hello with optional name parameter
server.tool(
"say_hello", // first argument: tool name
"Say hello to someone or everyone", // second argument: tool description
{
name: z
.string()
.optional()
.describe("Name of the person to greet (optional)"),
}, // third argument: tool parameters schema (using zod)
async ({ name }) => {
console.error(
new Error("say_hello tool invoked", {
cause: "for logging demonstration purposes",
}),
);
console.log("say_hello tool invoked with name:", name);
// fourth argument: tool implementation
try {
const greeting = name ? `Hello, ${name}!` : "Hello, world!";
return {
content: [
{
type: "text",
text: `${greeting} This message is from an MCP tool! 🎉`,
},
],
};
} catch (err) {
// Log error to dashboard with full error object
console.error("say_hello tool error:", err);
throw err;
}
},
);
};