-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-server.js
More file actions
254 lines (241 loc) · 7.18 KB
/
Copy pathmcp-server.js
File metadata and controls
254 lines (241 loc) · 7.18 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import {
getStats,
getPricingConfig,
updatePricingConfig,
refreshStatsCache,
} from "./stats-service.js";
/**
* 创建一个配置完成的 MCP Server 实例(不连接 stdio)。
* 返回的实例带有 `__handlers = { listTools, callTool }`,
* 这是为测试开的“逃生门”:MCP SDK 未暴露已注册 handler 的公共 API,
* 因此直接挂在实例上以便单元/集成测试绕过 stdio 传输直接调用。
*/
export function createMcpServer() {
const server = new Server(
{
name: "openclaw-usage-stats",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
const listToolsHandler = async () => ({
tools: [
{
name: "get_total_usage",
description: "获取 OpenClaw 总体 Token 用量与费用汇总 / Get overall OpenClaw token usage and cost summary.",
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "get_usage_by_provider",
description: "按 LLM 提供商查看用量与费用明细 / Get usage and cost breakdown by LLM provider (e.g. openai, anthropic, minimax).",
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "get_usage_by_model",
description: "按具体模型查看用量与费用明细 / Get usage and cost breakdown by specific models.",
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "list_recent_sessions",
description: "列出最近会话及其用量统计 / List the most recent sessions with usage statistics.",
inputSchema: {
type: "object",
properties: {
limit: { type: "number", description: "Number of sessions to return", default: 10 },
},
},
},
{
name: "get_session_stats",
description: "获取指定会话 ID 的详细用量统计 / Get detailed usage statistics for a specific session ID (usually UUID).",
inputSchema: {
type: "object",
properties: {
sessionId: { type: "string", description: "The UUID of the session" },
},
required: ["sessionId"],
},
},
{
name: "get_pricing_config",
description: "读取当前价格配置 / Get current pricing configuration.",
inputSchema: {
type: "object",
properties: {},
},
},
{
name: "update_pricing_config",
description: "更新价格配置并失效缓存 / Update pricing configuration and invalidate cached stats.",
inputSchema: {
type: "object",
properties: {
config: {
type: "object",
description: "完整价格配置对象 / Full pricing configuration object",
},
},
required: ["config"],
},
},
{
name: "refresh_stats_cache",
description: "主动刷新统计缓存 / Force refresh aggregated stats cache.",
inputSchema: {
type: "object",
properties: {},
},
},
],
});
const callToolHandler = async (request) => {
const { name, arguments: args } = request.params;
const data = await getStats();
try {
switch (name) {
case "get_total_usage": {
return {
content: [
{
type: "text",
text: JSON.stringify(data.summary, null, 2),
},
],
};
}
case "get_usage_by_provider": {
return {
content: [
{
type: "text",
text: JSON.stringify(data.byProvider, null, 2),
},
],
};
}
case "get_usage_by_model": {
const sortedModels = Object.values(data.byModel)
.sort((a, b) => b.totalTokens - a.totalTokens);
return {
content: [
{
type: "text",
text: JSON.stringify(sortedModels, null, 2),
},
],
};
}
case "list_recent_sessions": {
const limit = args?.limit || 10;
const recent = data.sessions.slice(0, limit).map(s => ({
id: s.id,
status: s.status,
providers: s.providers,
models: s.models,
totalTokens: s.totalTokens,
totalCost: s.totalCost,
lastActive: s.lastTimestamp,
}));
return {
content: [
{
type: "text",
text: JSON.stringify(recent, null, 2),
},
],
};
}
case "get_session_stats": {
const { sessionId } = args;
const session = data.sessions.find(s => s.id === sessionId);
if (!session) {
return {
content: [{ type: "text", text: `Session with ID ${sessionId} not found.` }],
isError: true,
};
}
return {
content: [
{
type: "text",
text: JSON.stringify(session, null, 2),
},
],
};
}
case "get_pricing_config": {
const config = await getPricingConfig();
return {
content: [
{
type: "text",
text: JSON.stringify(config, null, 2),
},
],
};
}
case "update_pricing_config": {
const result = await updatePricingConfig(args.config);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "refresh_stats_cache": {
const result = await refreshStatsCache();
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
default:
throw new Error(`Tool not found: ${name}`);
}
} catch (error) {
return {
content: [{ type: "text", text: `Error: ${error.message}` }],
isError: true,
};
}
};
server.setRequestHandler(ListToolsRequestSchema, listToolsHandler);
server.setRequestHandler(CallToolRequestSchema, callToolHandler);
server.__handlers = { listTools: listToolsHandler, callTool: callToolHandler };
return server;
}
async function main() {
const server = createMcpServer();
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("OpenClaw Usage MCP server running on stdio");
}
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error) => {
console.error("Fatal error in MCP server:", error);
process.exit(1);
});
}