-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapps.ts
More file actions
213 lines (207 loc) · 7.47 KB
/
Copy pathapps.ts
File metadata and controls
213 lines (207 loc) · 7.47 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
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { createKernelClient } from "@/lib/mcp/kernel-client";
import { registerJsonResourceTemplate } from "@/lib/mcp/resource-templates";
import {
errorResponse,
jsonResponse,
paginatedJsonResponse,
textResponse,
toolErrorResponse,
} from "@/lib/mcp/responses";
import { paginationParams } from "@/lib/mcp/schemas";
export function registerAppCapabilities(server: McpServer) {
server.resource("apps", "apps://", async (uri, extra) => {
if (!extra.authInfo) {
throw new Error("Authentication required");
}
const client = createKernelClient(extra.authInfo.token);
const appsPage = await client.apps.list();
const items = appsPage.getPaginatedItems();
return {
contents: [
{
uri: uri.toString(),
mimeType: "application/json",
text:
items.length > 0 ? JSON.stringify(items, null, 2) : "No apps found",
},
],
};
});
registerJsonResourceTemplate(server, {
name: "app",
uriTemplate: "apps://{appName}",
variableName: "appName",
resourceLabel: "App",
read: async (client, appName) => {
const appsPage = await client.apps.list({ app_name: appName });
return appsPage.getPaginatedItems()[0];
},
});
// manage_apps -- List apps, invoke actions, manage deployments, check invocations
server.tool(
"manage_apps",
'Manage Kernel apps when an agent needs to discover deployed app actions, invoke an app, or inspect deployment/invocation state. Use "list_apps" before invoking an unknown app, "invoke" to run an action, get/list actions to inspect results, and "delete_deployment" to remove a deployment.',
{
action: z
.enum([
"list_apps",
"invoke",
"get_deployment",
"list_deployments",
"delete_deployment",
"get_invocation",
])
.describe("Operation to perform."),
app_name: z
.string()
.describe(
"(list_apps, invoke, list_deployments) App name filter or target.",
)
.optional(),
version: z
.string()
.describe(
"(list_apps, invoke, list_deployments) App version filter. Defaults to 'latest' for invoke. Deployment version filtering requires app_name.",
)
.optional(),
query: z.string().describe("(list_apps) Search apps by name.").optional(),
action_name: z
.string()
.describe("(invoke) Action to execute within the app.")
.optional(),
payload: z
.string()
.describe("(invoke) JSON string with action parameters.")
.optional(),
deployment_id: z
.string()
.describe("(get_deployment, delete_deployment) Deployment ID.")
.optional(),
invocation_id: z
.string()
.describe("(get_invocation) Invocation ID to retrieve.")
.optional(),
...paginationParams,
},
{
title: "Manage Kernel apps and invocations",
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
openWorldHint: true,
},
async (params, extra) => {
if (!extra.authInfo) throw new Error("Authentication required");
const client = createKernelClient(extra.authInfo.token);
try {
switch (params.action) {
case "list_apps": {
const page = await client.apps.list({
...(params.app_name && { app_name: params.app_name }),
...(params.version && { version: params.version }),
...(params.query && { query: params.query }),
...(params.limit !== undefined && { limit: params.limit }),
...(params.offset !== undefined && { offset: params.offset }),
});
return paginatedJsonResponse(page);
}
case "invoke": {
if (!params.app_name || !params.action_name) {
return errorResponse(
"Error: app_name and action_name are required for invoke.",
);
}
const invocation = await client.invocations.create({
app_name: params.app_name,
action_name: params.action_name,
payload: params.payload,
version: params.version ?? "latest",
async: true,
});
if (!invocation)
return errorResponse("Failed to create invocation");
const stream = await client.invocations.follow(invocation.id);
let finalInvocation = invocation;
for await (const evt of stream) {
if (evt.event === "error") {
return errorResponse(
JSON.stringify(
{
status: "error",
invocation_id: invocation.id,
error: evt,
},
null,
2,
),
);
}
if (evt.event === "invocation_state") {
finalInvocation = evt.invocation || finalInvocation;
if (
finalInvocation.status === "succeeded" ||
finalInvocation.status === "failed"
)
break;
}
}
return jsonResponse(finalInvocation);
}
case "get_deployment": {
if (!params.deployment_id)
return errorResponse("Error: deployment_id is required.");
const deployment = await client.deployments.retrieve(
params.deployment_id,
);
if (!deployment)
return errorResponse(
`Deployment "${params.deployment_id}" not found`,
);
return jsonResponse(deployment);
}
case "list_deployments": {
if (params.version && !params.app_name) {
return errorResponse(
"Error: app_name is required when filtering deployments by version.",
);
}
const page = await client.deployments.list({
...(params.app_name && { app_name: params.app_name }),
...(params.version && { app_version: params.version }),
...(params.limit !== undefined && { limit: params.limit }),
...(params.offset !== undefined && { offset: params.offset }),
});
return paginatedJsonResponse(page);
}
case "delete_deployment": {
if (!params.deployment_id) {
return errorResponse(
"Error: deployment_id is required for delete_deployment.",
);
}
await client.deployments.delete(params.deployment_id);
return textResponse(
`Deployment "${params.deployment_id}" deleted successfully.`,
);
}
case "get_invocation": {
if (!params.invocation_id)
return errorResponse("Error: invocation_id is required.");
const invocation = await client.invocations.retrieve(
params.invocation_id,
);
if (!invocation)
return errorResponse(
`Invocation "${params.invocation_id}" not found`,
);
return jsonResponse(invocation);
}
}
} catch (error) {
return toolErrorResponse("manage_apps", params.action, error);
}
},
);
}