-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp.ts
More file actions
178 lines (163 loc) · 5.07 KB
/
Copy pathmcp.ts
File metadata and controls
178 lines (163 loc) · 5.07 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
import express from "express";
import { randomUUID } from "node:crypto";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
import { any, z } from "zod";
import * as YAML from "yaml";
import { getAuth } from "firebase-admin/auth";
import {
PortalService,
type Error,
type ApiHubApi,
type ApiHubApiVersion,
type ApiHubApiVersionSpecContents,
} from "apigee-portal-module";
export class McpUserService {
portalService: PortalService;
dataCache: {
apis: ApiHubApi[];
versions: { [key: string]: ApiHubApiVersion };
deployments: any;
specs: any;
} = {
apis: [],
versions: {},
deployments: {},
specs: {},
};
constructor(service: PortalService) {
this.portalService = service;
}
// Map to store transports by session ID
public transports: { [sessionId: string]: StreamableHTTPServerTransport } =
{};
public updateCache(newCache: {
apis: ApiHubApi[];
versions: { [key: string]: ApiHubApiVersion };
deployments: any;
specs: any;
}) {
this.dataCache = newCache;
}
public handleSessionRequest = async (
req: express.Request,
res: express.Response,
) => {
const sessionId = req.headers["mcp-session-id"] as string | undefined;
if (!sessionId || !this.transports[sessionId]) {
res.status(400).send("Invalid or missing session ID");
return;
}
const transport = this.transports[sessionId];
await transport.handleRequest(req, res);
};
public mcppost = async (req: express.Request, res: express.Response) => {
// Check for existing session ID
const sessionId = req.headers["mcp-session-id"] as string | undefined;
let transport: StreamableHTTPServerTransport;
if (sessionId && this.transports[sessionId]) {
// Reuse existing transport
transport = this.transports[sessionId];
} else if (!sessionId && isInitializeRequest(req.body)) {
// New initialization request
transport = new StreamableHTTPServerTransport({
sessionIdGenerator: () => randomUUID(),
onsessioninitialized: (sessionId) => {
// Store the transport by session ID
this.transports[sessionId] = transport;
},
// DNS rebinding protection is disabled by default for backwards compatibility. If you are running this server
// locally, make sure to set:
enableDnsRebindingProtection: false,
allowedHosts: ["127.0.0.1", "localhost:8080", "*"],
});
// Clean up transport when closed
transport.onclose = () => {
if (transport.sessionId) {
delete this.transports[transport.sessionId];
}
};
const server = new McpServer({
name: "apigee-user",
version: "3.0.5",
});
// appsList
server.registerTool(
"appsList",
{
title: "App Subscriptions List Tool",
description: "Lists all subscriptions to API products.",
inputSchema: {
email: z.string().describe("The email address of the user."),
},
},
async ({ email }) => {
let userEmail = email;
// try {
// const userInfo = await getAuth().verifyIdToken(token);
// console.log(userInfo);
// if (userInfo) userEmail = userInfo.email ?? "";
// } catch (e) {
// console.error("Could not verify user id token.");
// return {
// content: [
// {
// type: "text",
// text: `Could not verify the user.`,
// },
// ],
// };
// }
if (!userEmail) {
console.error("User email could not be found.");
return {
content: [
{
type: "text",
text: `Could not find the user.`,
},
],
};
} else {
let appsResponse = await this.portalService.getApps(userEmail);
if (appsResponse && appsResponse.data) {
return {
content: [
{
type: "text",
text: `${JSON.stringify(appsResponse.data)}`,
},
],
};
} else {
return {
content: [
{
type: "text",
text: `No apps found.`,
},
],
};
}
}
},
);
// Connect to the MCP server
await server.connect(transport);
} else {
// Invalid request
res.status(400).json({
jsonrpc: "2.0",
error: {
code: -32000,
message: "Bad Request: No valid session ID provided",
},
id: null,
});
return;
}
// Handle the request
await transport.handleRequest(req, res, req.body);
};
}