-
Notifications
You must be signed in to change notification settings - Fork 835
Expand file tree
/
Copy pathfetchMCPTools.ts
More file actions
96 lines (82 loc) · 2.82 KB
/
Copy pathfetchMCPTools.ts
File metadata and controls
96 lines (82 loc) · 2.82 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
import fetch from 'node-fetch'
import path from 'path'
import fs from 'fs'
const MCP_TOOLS_URL =
'https://raw.githubusercontent.com/PostHog/posthog/refs/heads/master/services/mcp/schema/tool-definitions-all.json'
interface MCPTool {
category?: string
summary: string
description?: string
required_scopes?: string[]
}
interface ToolCategory {
name: string
tools: Array<{
name: string
summary: string
}>
}
interface ToolByName {
summary: string
description?: string
category?: string
required_scopes?: string[]
}
export interface MCPToolsData {
categories: ToolCategory[] | null
byName: Record<string, ToolByName> | null
error: boolean
}
export async function fetchAndProcessMCPTools(): Promise<MCPToolsData> {
try {
const response = await fetch(MCP_TOOLS_URL)
if (response.status !== 200) {
throw new Error(`Failed to fetch MCP tools: ${response.status}`)
}
const mcpTools = (await response.json()) as Record<string, MCPTool>
// Process the tools into categories
const toolCategories: Record<string, Array<{ name: string; summary: string }>> = {}
const byName: Record<string, ToolByName> = {}
Object.entries(mcpTools).forEach(([toolName, toolDef]) => {
const category = toolDef.category || 'Uncategorized'
if (!toolCategories[category]) {
toolCategories[category] = []
}
toolCategories[category].push({
name: toolName,
summary: toolDef.summary,
})
byName[toolName] = {
summary: toolDef.summary,
description: toolDef.description,
category: toolDef.category,
required_scopes: toolDef.required_scopes,
}
})
// Sort tools within each category alphabetically
Object.values(toolCategories).forEach((tools) => {
tools.sort((a, b) => a.name.localeCompare(b.name))
})
// Convert to array format and sort categories
const categoriesArray = Object.entries(toolCategories)
.map(([name, tools]) => ({ name, tools }))
.sort((a, b) => a.name.localeCompare(b.name))
return {
categories: categoriesArray,
byName,
error: false,
}
} catch (error) {
console.error('Error fetching MCP tools:', error)
return {
categories: null,
byName: null,
error: true,
}
}
}
export function writeMCPToolsToFile(data: MCPToolsData): void {
const mcpToolsPath = path.resolve(__dirname, '../../src/data/mcp-tools.json')
fs.mkdirSync(path.dirname(mcpToolsPath), { recursive: true })
fs.writeFileSync(mcpToolsPath, JSON.stringify(data, null, 2))
}