forked from contextstream/mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.ts
More file actions
119 lines (113 loc) · 4.12 KB
/
Copy pathresources.ts
File metadata and controls
119 lines (113 loc) · 4.12 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
import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import type { ContextStreamClient } from "./client.js";
function wrapText(uri: string, text: string) {
return { contents: [{ uri, text }] };
}
function wrapResource(resource: {
uri: string;
name: string;
title?: string;
description?: string;
mimeType?: string;
}) {
return { resources: [resource] };
}
function extractItems<T>(payload: unknown): T[] {
if (!payload || typeof payload !== "object") return [];
const direct = (payload as Record<string, unknown>).items;
if (Array.isArray(direct)) return direct as T[];
const nested = (payload as Record<string, unknown>).data;
if (nested && typeof nested === "object") {
const nestedItems = (nested as Record<string, unknown>).items;
if (Array.isArray(nestedItems)) return nestedItems as T[];
if (Array.isArray(nested)) return nested as T[];
}
return [];
}
export function registerResources(server: McpServer, client: ContextStreamClient, apiUrl: string) {
// OpenAPI resource
server.registerResource(
"contextstream-openapi",
new ResourceTemplate("contextstream:openapi", {
list: () =>
wrapResource({
uri: "contextstream:openapi",
name: "contextstream-openapi",
title: "ContextStream OpenAPI",
description: "Machine-readable OpenAPI from the configured API endpoint",
mimeType: "application/json",
}),
}),
{
title: "ContextStream OpenAPI spec",
description: "Machine-readable OpenAPI from the configured API endpoint",
mimeType: "application/json",
},
async () => {
const uri = `${apiUrl.replace(/\/$/, "")}/api-docs/openapi.json`;
const res = await fetch(uri);
const text = await res.text();
return wrapText("contextstream:openapi", text);
}
);
// Workspaces list resource
server.registerResource(
"contextstream-workspaces",
new ResourceTemplate("contextstream:workspaces", {
list: () =>
wrapResource({
uri: "contextstream:workspaces",
name: "contextstream-workspaces",
title: "Workspaces",
description: "List of accessible workspaces",
mimeType: "application/json",
}),
}),
{ title: "Workspaces", description: "List of accessible workspaces" },
async () => {
const data = await client.listWorkspaces();
return wrapText("contextstream:workspaces", JSON.stringify(data, null, 2));
}
);
// Projects by workspace resource template
server.registerResource(
"contextstream-projects",
new ResourceTemplate("contextstream:projects/{workspaceId}", {
list: async () => {
try {
const workspaces = await client.listWorkspaces({ page_size: 50 });
const items = extractItems<{ id: string; name?: string }>(workspaces);
return {
resources: items.map((workspace) => ({
uri: `contextstream:projects/${workspace.id}`,
name: `contextstream-projects-${workspace.id}`,
title: workspace.name ? `Projects in ${workspace.name}` : "Projects in workspace",
description: "Projects in the specified workspace",
mimeType: "application/json",
})),
};
} catch {
return { resources: [] };
}
},
complete: {
workspaceId: async () => {
try {
const workspaces = await client.listWorkspaces({ page_size: 50 });
const items = extractItems<{ id: string }>(workspaces);
return items.map((workspace) => workspace.id);
} catch {
return [];
}
},
},
}),
{ title: "Projects for workspace", description: "Projects in the specified workspace" },
async (uri: URL, { workspaceId }: { workspaceId: string | string[] }) => {
const wsId = Array.isArray(workspaceId) ? workspaceId[0] : workspaceId;
const data = await client.listProjects({ workspace_id: wsId });
return wrapText(uri.href, JSON.stringify(data, null, 2));
}
);
}