-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathhandlers.ts
More file actions
166 lines (155 loc) · 5.36 KB
/
Copy pathhandlers.ts
File metadata and controls
166 lines (155 loc) · 5.36 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 { HttpApiBuilder } from "effect/unstable/httpapi";
import { Context, Effect } from "effect";
import { addGroup, capture } from "@executor-js/api";
import type { McpPluginExtension, McpProbeEndpointInput, McpServerInput } from "../sdk/plugin";
import { parseMcpIntegrationConfig } from "../sdk/types";
import { McpGroup } from "./group";
// ---------------------------------------------------------------------------
// Service tag — holds the raw extension shape the executor produces. Handlers
// wrap their generator bodies with `capture(...)` from `@executor-js/api`,
// which translates `StorageError` to `InternalError` at the edge.
// ---------------------------------------------------------------------------
export class McpExtensionService extends Context.Service<McpExtensionService, McpPluginExtension>()(
"McpExtensionService",
) {}
// ---------------------------------------------------------------------------
// Composed API
// ---------------------------------------------------------------------------
const ExecutorApiWithMcp = addGroup(McpGroup);
// ---------------------------------------------------------------------------
// Convert API payload → McpServerInput
// ---------------------------------------------------------------------------
const toServerInput = (
payload: { transport?: "remote" | "stdio" } & Record<string, unknown>,
): McpServerInput => {
if (payload.transport === "stdio") {
const p = payload as {
transport: "stdio";
name: string;
description?: string;
command: string;
args?: readonly string[];
envVars?: readonly string[];
env?: Record<string, string>;
cwd?: string;
slug?: string;
};
return {
transport: "stdio",
name: p.name,
description: p.description,
command: p.command,
args: p.args ? [...p.args] : undefined,
envVars: p.envVars ? [...p.envVars] : undefined,
env: p.env,
cwd: p.cwd,
slug: p.slug,
};
}
const p = payload as {
transport?: "remote";
name: string;
description?: string;
endpoint: string;
remoteTransport?: "streamable-http" | "sse" | "auto";
queryParams?: Record<string, string>;
headers?: Record<string, string>;
slug?: string;
authenticationTemplate?: McpServerInput extends { authenticationTemplate?: infer T }
? T
: never;
auth?: McpServerInput extends { auth?: infer A } ? A : never;
};
return {
transport: "remote",
name: p.name,
description: p.description,
endpoint: p.endpoint,
remoteTransport: p.remoteTransport,
queryParams: p.queryParams,
headers: p.headers,
slug: p.slug,
authenticationTemplate: p.authenticationTemplate,
auth: p.auth,
};
};
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
export const McpHandlers = HttpApiBuilder.group(ExecutorApiWithMcp, "mcp", (handlers) =>
handlers
.handle("probeEndpoint", ({ payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
return yield* ext.probeEndpoint(payload as McpProbeEndpointInput);
}),
),
)
.handle("addServer", ({ payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
return yield* ext.addServer(
toServerInput(payload as Parameters<typeof toServerInput>[0]),
);
}),
),
)
.handle("removeServer", ({ params: path }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
yield* ext.removeServer(path.slug);
return { removed: true };
}),
),
)
.handle("getServer", ({ params: path }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
const integration = yield* ext.getServer(path.slug);
if (integration === null) return null;
const config = parseMcpIntegrationConfig(integration.config);
if (config === null) return null;
return {
slug: integration.slug,
description: integration.description,
kind: integration.kind,
canRemove: integration.canRemove,
canRefresh: integration.canRefresh,
config,
};
}),
),
)
.handle("configureServer", ({ params: path, payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
return yield* ext.configureServer(path.slug, payload.config);
}),
),
)
.handle("refreshServerTools", ({ params: path }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
return yield* ext.refreshServerTools(path.slug);
}),
),
)
.handle("configureAuth", ({ params: path, payload }) =>
capture(
Effect.gen(function* () {
const ext = yield* McpExtensionService;
const authenticationTemplate = yield* ext.configureAuth(path.slug, {
authenticationTemplate: payload.authenticationTemplate,
mode: payload.mode ?? "merge",
});
return { authenticationTemplate: [...authenticationTemplate] };
}),
),
),
);