Skip to content

Commit da62f30

Browse files
committed
wip sse
1 parent 5f45a7d commit da62f30

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

src/client.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
StdioClientTransport,
44
StdioServerParameters,
55
} from "@modelcontextprotocol/sdk/client/stdio.js";
6+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
67
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
8+
import { ServerParameters } from "./fetch-metamcp.js";
79

810
const sleep = (time: number) =>
911
new Promise<void>((resolve) => setTimeout(() => resolve(), time));
@@ -13,9 +15,27 @@ export interface ConnectedClient {
1315
}
1416

1517
export const createMetaMcpClient = (
16-
serverParams: StdioServerParameters
18+
serverParams: ServerParameters
1719
): { client: Client | undefined; transport: Transport | undefined } => {
18-
const transport = new StdioClientTransport(serverParams);
20+
let transport: Transport | undefined;
21+
22+
// Create the appropriate transport based on server type
23+
if (serverParams.type === "STDIO") {
24+
const stdioParams: StdioServerParameters = {
25+
command: serverParams.command || "",
26+
args: serverParams.args || undefined,
27+
env: serverParams.env || undefined,
28+
// Use default values for other optional properties
29+
// stderr and cwd will use their default values
30+
};
31+
transport = new StdioClientTransport(stdioParams);
32+
} else if (serverParams.type === "SSE" && serverParams.url) {
33+
transport = new SSEClientTransport(new URL(serverParams.url));
34+
} else {
35+
console.error(`Unsupported server type: ${serverParams.type}`);
36+
return { client: undefined, transport: undefined };
37+
}
38+
1939
const client = new Client(
2040
{
2141
name: "MetaMCP",

src/fetch-metamcp.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import axios from "axios";
22
import { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
3+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
34
import {
45
getDefaultEnvironment,
56
getMetaMcpApiBaseUrl,
67
getMetaMcpApiKey,
78
} from "./utils.js";
89

9-
let _mcpServersCache: Record<string, StdioServerParameters> | null = null;
10+
// Define a new interface for server parameters that can be either STDIO or SSE
11+
export interface ServerParameters {
12+
uuid: string;
13+
name: string;
14+
description: string;
15+
type: "STDIO" | "SSE";
16+
command?: string | null;
17+
args?: string[] | null;
18+
env?: Record<string, string> | null;
19+
url?: string | null;
20+
created_at: string;
21+
profile_uuid: string;
22+
status: string;
23+
}
24+
25+
let _mcpServersCache: Record<string, ServerParameters> | null = null;
1026
let _mcpServersCacheTimestamp: number = 0;
1127
const CACHE_TTL_MS = 1000; // 1 second cache TTL
1228

1329
export async function getMcpServers(
1430
forceRefresh: boolean = false
15-
): Promise<Record<string, StdioServerParameters>> {
31+
): Promise<Record<string, ServerParameters>> {
1632
const currentTime = Date.now();
1733
const cacheAge = currentTime - _mcpServersCacheTimestamp;
1834

@@ -40,27 +56,29 @@ export async function getMcpServers(
4056
});
4157
const data = response.data;
4258

43-
const serverDict: Record<string, StdioServerParameters> = {};
59+
const serverDict: Record<string, ServerParameters> = {};
4460
for (const params of data) {
45-
if ("args" in params && !params.args) {
46-
params.args = undefined;
47-
}
61+
// Process based on server type
62+
if (params.type === "STDIO") {
63+
if ("args" in params && !params.args) {
64+
params.args = undefined;
65+
}
4866

49-
params.env = {
50-
...getDefaultEnvironment(),
51-
...(params.env || {}),
52-
};
53-
54-
const serverParams: StdioServerParameters = {
55-
...params,
56-
env: {
67+
params.env = {
5768
...getDefaultEnvironment(),
5869
...(params.env || {}),
59-
},
60-
};
70+
};
71+
} else if (params.type === "SSE") {
72+
// For SSE servers, ensure url is present
73+
if (!params.url) {
74+
console.warn(`SSE server ${params.uuid} is missing url field, skipping`);
75+
continue;
76+
}
77+
}
78+
6179
const uuid = params.uuid;
6280
if (uuid) {
63-
serverDict[uuid] = serverParams;
81+
serverDict[uuid] = params;
6482
}
6583
}
6684

src/sessions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
2+
import { ServerParameters } from "./fetch-metamcp.js";
23
import {
34
ConnectedClient,
45
createMetaMcpClient,
@@ -10,7 +11,7 @@ const _sessions: Record<string, ConnectedClient> = {};
1011
export const getSession = async (
1112
sessionKey: string,
1213
uuid: string,
13-
params: StdioServerParameters
14+
params: ServerParameters
1415
): Promise<ConnectedClient | undefined> => {
1516
if (sessionKey in _sessions) {
1617
return _sessions[sessionKey];

src/utils.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
2+
import { ServerParameters } from "./fetch-metamcp.js";
23
import crypto from "crypto";
34

45
/**
@@ -64,27 +65,40 @@ export function sanitizeName(name: string): string {
6465
}
6566

6667
export function computeParamsHash(
67-
params: StdioServerParameters,
68+
params: ServerParameters,
6869
uuid: string
6970
): string {
70-
const paramsDict = {
71-
uuid,
72-
command: params.command,
73-
args: params.args,
74-
env: params.env
75-
? Object.fromEntries(
76-
Object.entries(params.env).sort((a, b) => a[0].localeCompare(b[0]))
77-
)
78-
: null,
79-
};
71+
let paramsDict: any;
72+
73+
if (params.type === "STDIO") {
74+
paramsDict = {
75+
uuid,
76+
type: params.type,
77+
command: params.command,
78+
args: params.args,
79+
env: params.env
80+
? Object.fromEntries(
81+
Object.entries(params.env).sort((a, b) => a[0].localeCompare(b[0]))
82+
)
83+
: null,
84+
};
85+
} else if (params.type === "SSE") {
86+
paramsDict = {
87+
uuid,
88+
type: params.type,
89+
url: params.url,
90+
};
91+
} else {
92+
throw new Error(`Unsupported server type: ${params.type}`);
93+
}
8094

8195
const paramsJson = JSON.stringify(paramsDict);
8296
return crypto.createHash("sha256").update(paramsJson).digest("hex");
8397
}
8498

8599
export function getSessionKey(
86100
uuid: string,
87-
params: StdioServerParameters
101+
params: ServerParameters
88102
): string {
89103
return `${uuid}_${computeParamsHash(params, uuid)}`;
90104
}

0 commit comments

Comments
 (0)