Spaces:
Sleeping
Sleeping
File size: 3,347 Bytes
d4f5807 |
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 |
#!/usr/bin/env node
import {
Client,
ListResourcesResultSchema,
ListToolsResultSchema,
NodeOAuthClientProvider,
connectToRemoteServer,
createLazyAuthCoordinator,
getServerUrlHash,
log,
parseCommandLineArgs,
setupSignalHandlers,
version
} from "./chunk-4PNJQ7WT.js";
// src/client.ts
import { EventEmitter } from "events";
async function runClient(serverUrl, callbackPort, headers, transportStrategy = "http-first", host, staticOAuthClientMetadata, staticOAuthClientInfo) {
const events = new EventEmitter();
const serverUrlHash = getServerUrlHash(serverUrl);
const authCoordinator = createLazyAuthCoordinator(serverUrlHash, callbackPort, events);
const authProvider = new NodeOAuthClientProvider({
serverUrl,
callbackPort,
host,
clientName: "MCP CLI Client",
staticOAuthClientMetadata,
staticOAuthClientInfo
});
const client = new Client(
{
name: "mcp-remote",
version
},
{
capabilities: {}
}
);
let server = null;
const authInitializer = async () => {
const authState = await authCoordinator.initializeAuth();
server = authState.server;
if (authState.skipBrowserAuth) {
log("Authentication was completed by another instance - will use tokens from disk...");
await new Promise((res) => setTimeout(res, 1e3));
}
return {
waitForAuthCode: authState.waitForAuthCode,
skipBrowserAuth: authState.skipBrowserAuth
};
};
try {
const transport = await connectToRemoteServer(client, serverUrl, authProvider, headers, authInitializer, transportStrategy);
transport.onmessage = (message) => {
log("Received message:", JSON.stringify(message, null, 2));
};
transport.onerror = (error) => {
log("Transport error:", error);
};
transport.onclose = () => {
log("Connection closed.");
process.exit(0);
};
const cleanup = async () => {
log("\nClosing connection...");
await client.close();
if (server) {
server.close();
}
};
setupSignalHandlers(cleanup);
log("Connected successfully!");
try {
log("Requesting tools list...");
const tools = await client.request({ method: "tools/list" }, ListToolsResultSchema);
log("Tools:", JSON.stringify(tools, null, 2));
} catch (e) {
log("Error requesting tools list:", e);
}
try {
log("Requesting resource list...");
const resources = await client.request({ method: "resources/list" }, ListResourcesResultSchema);
log("Resources:", JSON.stringify(resources, null, 2));
} catch (e) {
log("Error requesting resources list:", e);
}
log("Exiting OK...");
if (server) {
server.close();
}
process.exit(0);
} catch (error) {
log("Fatal error:", error);
if (server) {
server.close();
}
process.exit(1);
}
}
parseCommandLineArgs(process.argv.slice(2), "Usage: npx tsx client.ts <https://server-url> [callback-port] [--debug]").then(({ serverUrl, callbackPort, headers, transportStrategy, host, staticOAuthClientMetadata, staticOAuthClientInfo }) => {
return runClient(serverUrl, callbackPort, headers, transportStrategy, host, staticOAuthClientMetadata, staticOAuthClientInfo);
}).catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});
|