Repository navigation
Expand file tree
/
Copy pathexample-usage.mjs
More file actions
65 lines (58 loc) · 3.47 KB
/
Copy pathexample-usage.mjs
File metadata and controls
65 lines (58 loc) · 3.47 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
#!/usr/bin/env node
// Standalone quickstart for O2Switch AI — uses ONLY the official MCP SDK (@modelcontextprotocol/sdk),
// no dependency on any private code. Run it with:
//
// npm install @modelcontextprotocol/sdk
// node example-usage.mjs
//
// What this actually shows: the server (https://api-plugins.com/mcp) requires OAuth — every real
// user connects through ChatGPT's or Claude's own "Add custom connector" flow, which handles the
// browser-based authorization for you. There is no standalone/scriptable login (deliberately: your
// cPanel token is never handled outside that flow). So this script does two honest things instead
// of pretending to bypass that:
//
// 1. Fetches the server's public OAuth discovery metadata (no auth required for this part) —
// real, live data straight from the server, showing exactly what it advertises.
// 2. Attempts to list tools with the MCP SDK the same way a real client would, and shows you the
// real 401 challenge the server sends back — which is what tells a client like ChatGPT or
// Claude to start the OAuth flow.
//
// To actually use the tools, add the connector for real: see the "Se connecter" section in
// README.md.
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const SERVER_URL = "https://api-plugins.com/mcp";
async function main() {
console.log(`O2Switch AI — quickstart\n`);
console.log(`1. Fetching public OAuth discovery metadata...\n`);
const metadataUrl = new URL("/.well-known/oauth-authorization-server", SERVER_URL);
const metadataResponse = await fetch(metadataUrl);
const metadata = await metadataResponse.json();
console.log(` issuer: ${metadata.issuer}`);
console.log(` authorization_endpoint: ${metadata.authorization_endpoint}`);
console.log(` token_endpoint: ${metadata.token_endpoint}`);
console.log(` grant_types_supported: ${metadata.grant_types_supported.join(", ")}`);
console.log(` (this is real, live data from the server — not hardcoded here)\n`);
console.log(`2. Attempting to connect without a token, the same way a real MCP client first tries...\n`);
try {
const client = new Client({ name: "plugins-o2switch-example", version: "1.0.0" });
await client.connect(new StreamableHTTPClientTransport(new URL(SERVER_URL)));
const { tools } = await client.listTools();
// Only reachable if the server ever allows unauthenticated access — it doesn't today, but the
// real tool list would print here if it did.
console.log(` Connected without auth (unexpected) — ${tools.length} tools available.`);
await client.close();
} catch (error) {
console.log(` As expected: the server requires OAuth. Real error:\n`);
console.log(` ${error instanceof Error ? error.message : String(error)}\n`);
console.log(` This is exactly the signal ChatGPT/Claude use to start their own "Connect" flow —`);
console.log(` there's no way to script past it, by design: your cPanel token only ever goes`);
console.log(` through that flow, never through a script like this one.\n`);
}
console.log(`To actually use the tools: add https://api-plugins.com/mcp as a custom connector in`);
console.log(`ChatGPT or Claude (see README.md) — they handle the OAuth flow for you.`);
}
main().catch((error) => {
console.error(error instanceof Error ? error.stack : error);
process.exitCode = 1;
});