cred402/mcp/server_sdk.ts
caelum0x 2d43618725 feat: real integrations (Casper Testnet, x402/EIP-712, MCP, RealFi, cross-chain), wallet sign-in, branding
Replace the design-note docs (p1–p7) with the implemented "make it real" phase
docs (p8–p10) and wire production-grade, env-gated real integrations:

- p8 Casper Testnet: byte-exact deploys + WASM install via casper-js-sdk; live
  node reads (node.testnet.casper.network); CRED402_CHAIN=sim|testnet transport.
- p9 x402 + data + MCP: real EIP-712 typed-data digests (@casper-ecosystem/
  casper-eip-712); casper-x402 facilitator client (x402 V2); live Open-Meteo GHI;
  official @modelcontextprotocol/sdk server; casper-sidecar SSE event feed.
- p10 RealFi + cross-chain: real Stripe test-mode webhooks (HMAC-verified) and
  Plaid sandbox → privacy-preserving on-chain envelopes; Base Sepolia EVM deploy
  (Foundry); reachable via /api/realfi/* and /api/x402/facilitator routes.
- Sign-in with Casper Wallet: real extension provider + challenge/ed25519/session
  flow (/v1/auth/wallet/*), console connect + sign-in button.
- Branding: Cred402 logo mark, favicon, OG image; Vercel config for the console.

All env-gated with sim/free-API fallbacks; full suite green, all toolchains
(TS/Rust/Go/Solidity) build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 00:37:36 +03:00

75 lines
2.9 KiB
TypeScript

/**
* Cred402 MCP server — official SDK transport (p9).
*
* The same tool + resource registry as the zero-dep `mcp/server.ts`, but served
* through the official `@modelcontextprotocol/sdk` low-level `Server` over stdio.
* This is the standards-compliant entry point real MCP clients (Claude Desktop,
* MCP Inspector) connect to — same tools, real SDK framing, capability
* negotiation, and error semantics handled by the SDK.
*
* npx tsx mcp/server_sdk.ts
*
* Claude Desktop config:
* { "command": "npx", "args": ["tsx", "mcp/server_sdk.ts"], "cwd": "/path/to/cred402" }
*/
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { Cred402Economy } from "../agents/index.js";
import { TOOLS, TOOL_INDEX } from "./tools.js";
import { listResources, readResource } from "./resources.js";
const econ = new Cred402Economy();
econ.bootstrap();
econ.createJob();
const server = new Server(
{ name: "cred402-mcp", version: "0.1.0" },
{
capabilities: { tools: {}, resources: {} },
instructions:
"Cred402 is a credit, reputation and x402-receipt protocol for autonomous RWA agents on Casper. " +
"Use the cred402.* tools to register agents, buy/sell evidence over x402, underwrite credit, draw/repay, and open disputes.",
},
);
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: TOOLS.map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema })),
}));
server.setRequestHandler(CallToolRequestSchema, async (req) => {
const tool = TOOL_INDEX.get(req.params.name);
if (!tool) {
return { isError: true, content: [{ type: "text" as const, text: `unknown tool: ${req.params.name}` }] };
}
try {
const out = await tool.handler((req.params.arguments as Record<string, unknown>) ?? {}, econ);
return { content: [{ type: "text" as const, text: JSON.stringify(out, null, 2) }] };
} catch (err) {
return { isError: true, content: [{ type: "text" as const, text: `error: ${(err as Error).message}` }] };
}
});
server.setRequestHandler(ListResourcesRequestSchema, async () => ({ resources: listResources(econ) }));
server.setRequestHandler(ReadResourceRequestSchema, async (req) => {
const res = readResource(req.params.uri, econ);
if (!res) throw new Error(`unknown resource: ${req.params.uri}`);
return { contents: [{ uri: req.params.uri, mimeType: "application/json", text: res.text }] };
});
async function main(): Promise<void> {
await server.connect(new StdioServerTransport());
process.stderr.write(`cred402-mcp (official SDK) ready — ${TOOLS.length} tools\n`);
}
main().catch((err) => {
process.stderr.write(`cred402-mcp fatal: ${(err as Error).message}\n`);
process.exit(1);
});