-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi-keys.ts
More file actions
166 lines (164 loc) · 5.37 KB
/
Copy pathapi-keys.ts
File metadata and controls
166 lines (164 loc) · 5.37 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
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { createKernelClient } from "@/lib/mcp/kernel-client";
export function registerAPIKeyCapabilities(server: McpServer) {
// manage_api_keys -- Create, list, get, update, and delete Kernel API keys
server.tool(
"manage_api_keys",
'Manage Kernel API keys. Use "create" to create an org-wide or project-scoped key, "list" to discover masked keys, "get" to retrieve one masked key, "update" to rename a key, or "delete" to revoke a key. Created keys include the plaintext key once.',
{
action: z
.enum(["create", "list", "get", "update", "delete"])
.describe("Operation to perform."),
api_key_id: z
.string()
.describe("API key ID. Required for get, update, and delete.")
.optional(),
name: z.string().describe("(create, update) API key name.").optional(),
project_id: z
.string()
.nullable()
.describe(
"(create) Project ID for project-scoped keys. Omit or use null for org-wide keys.",
)
.optional(),
days_to_expire: z
.number()
.int()
.min(1)
.max(3650)
.nullable()
.describe(
"(create) Days until expiry, up to 3650. Use null for no expiry.",
)
.optional(),
limit: z.number().describe("(list) Max results per page.").optional(),
offset: z.number().describe("(list) Pagination offset.").optional(),
},
async (params, extra) => {
if (!extra.authInfo) throw new Error("Authentication required");
const client = createKernelClient(extra.authInfo.token);
try {
switch (params.action) {
case "create": {
if (!params.name) {
return {
content: [
{ type: "text", text: "Error: name is required for create." },
],
};
}
const createParams: Parameters<typeof client.apiKeys.create>[0] = {
name: params.name,
};
if (params.project_id !== undefined) {
createParams.project_id = params.project_id;
}
if (params.days_to_expire !== undefined) {
createParams.days_to_expire = params.days_to_expire;
}
const apiKey = await client.apiKeys.create(createParams);
return {
content: [
{ type: "text", text: JSON.stringify(apiKey, null, 2) },
],
};
}
case "list": {
const page = await client.apiKeys.list({
...(params.limit !== undefined && { limit: params.limit }),
...(params.offset !== undefined && { offset: params.offset }),
});
const items = page.getPaginatedItems();
return {
content: [
{
type: "text",
text: JSON.stringify(
{
items,
has_more: page.has_more,
next_offset: page.next_offset,
},
null,
2,
),
},
],
};
}
case "get": {
if (!params.api_key_id) {
return {
content: [
{
type: "text",
text: "Error: api_key_id is required for get.",
},
],
};
}
const apiKey = await client.apiKeys.retrieve(params.api_key_id);
return {
content: [
{ type: "text", text: JSON.stringify(apiKey, null, 2) },
],
};
}
case "update": {
if (!params.api_key_id) {
return {
content: [
{
type: "text",
text: "Error: api_key_id is required for update.",
},
],
};
}
if (!params.name) {
return {
content: [
{ type: "text", text: "Error: name is required for update." },
],
};
}
const apiKey = await client.apiKeys.update(params.api_key_id, {
name: params.name,
});
return {
content: [
{ type: "text", text: JSON.stringify(apiKey, null, 2) },
],
};
}
case "delete": {
if (!params.api_key_id) {
return {
content: [
{
type: "text",
text: "Error: api_key_id is required for delete.",
},
],
};
}
await client.apiKeys.delete(params.api_key_id);
return {
content: [{ type: "text", text: "API key deleted successfully" }],
};
}
}
} catch (error) {
return {
content: [
{
type: "text",
text: `Error in manage_api_keys (${params.action}): ${error instanceof Error ? error.message : String(error)}`,
},
],
};
}
},
);
}