@forcedream/mcp-server
Advanced tools
| import { z } from 'zod'; | ||
| const FD_API = process.env.FD_API_BASE || 'https://api.forcedream.ai'; | ||
| const SLUG = 'code-generation-v1'; | ||
| /** | ||
| * Zod input schema for the forcedream_generate_code tool. task_description is required; | ||
| * max_wait_seconds bounds how long this call polls before returning a pollable task_id. | ||
| * A dedicated, named tool for ForceDream's code-generation-v1 agent, rather than requiring | ||
| * a caller to know the generic forcedream_invoke_agent + agent_slug pattern. | ||
| */ | ||
| export const generateCodeSchema = { | ||
| task_description: z.string().describe('What code to generate, e.g. "Write a Python function to validate an email address, with tests."'), | ||
| max_wait_seconds: z.number().optional().describe('Max seconds to poll (default 60, agent typically takes up to 55s given generation plus 6-module verification). On timeout, returns task_id to poll later.'), | ||
| }; | ||
| function authHeader() { | ||
| const key = process.env.FD_API_KEY || ''; | ||
| return key ? { Authorization: `Bearer ${key}` } : {}; | ||
| } | ||
| async function postJson(url, body) { | ||
| const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', ...authHeader() }, body: JSON.stringify(body) }); | ||
| let json = null; | ||
| try { | ||
| json = await res.json(); | ||
| } | ||
| catch { } | ||
| return { status: res.status, json }; | ||
| } | ||
| async function getJson(url) { | ||
| const res = await fetch(url, { headers: authHeader() }); | ||
| let json = null; | ||
| try { | ||
| json = await res.json(); | ||
| } | ||
| catch { } | ||
| return { status: res.status, json }; | ||
| } | ||
| const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); | ||
| // Invoke code-generation-v1 and wait (bounded) for the result. SPENDS balance -- needs | ||
| // FD_API_KEY. Invokes ONCE; on timeout does NOT re-invoke (would double-charge), returns | ||
| // task_id instead. Same real invoke/poll pattern as the other dedicated tools. | ||
| /** | ||
| * Invokes ForceDream's real code-generation-v1 agent and polls (bounded) for the result. | ||
| * Real generation, verified by 6 independent modules (syntax, dependencies, security, | ||
| * complexity, documentation, tests) -- never a fabricated pass. SPENDS your balance -- | ||
| * requires FD_API_KEY. Invokes once; never re-invokes on timeout (would double-charge) -- | ||
| * returns a pollable task_id instead. Set FD_MOCK_MODE=true to test without spending real | ||
| * balance. | ||
| * @param args.task_description - What code to generate. | ||
| * @param args.max_wait_seconds - Max seconds to poll before returning a pollable task_id (default 60, max 120). | ||
| */ | ||
| export async function generateCode(args) { | ||
| if (process.env.FD_MOCK_MODE === 'true') { | ||
| return { | ||
| status: 'completed', | ||
| agent: SLUG, | ||
| task_id: 'mock_' + Date.now(), | ||
| output: { generated_software: null, engineering_verification: null, engineering_assessment: null, note: 'Synthetic mock output. This is not real generated code or real verification.' }, | ||
| charged_pence: 0, | ||
| mock: true, | ||
| message: 'MOCK MODE ACTIVE (FD_MOCK_MODE=true): no real code was generated or verified, no balance was spent, and this response has no real proof_id -- forcedream_verify_proof will correctly reject it if you try. Unset FD_MOCK_MODE to run for real.', | ||
| }; | ||
| } | ||
| if (!process.env.FD_API_KEY) { | ||
| return { status: 'error', agent: SLUG, message: 'FD_API_KEY is required to generate (this spends your balance). Set it in the MCP server env. forcedream_search_agents and forcedream_verify_proof need no key.' }; | ||
| } | ||
| const maxWaitMs = Math.max(5, Math.min(120, args.max_wait_seconds ?? 60)) * 1000; | ||
| const inv = await postJson(`${FD_API}/v1/agents/${SLUG}/invoke`, { task: args.task_description }); | ||
| if (inv.status === 401) | ||
| return { status: 'error', agent: SLUG, message: 'Invalid FD_API_KEY (401). Check the key in the MCP server env.' }; | ||
| if (!inv.json?.task_id) | ||
| return { status: 'error', agent: SLUG, message: `Invoke failed (HTTP ${inv.status}): ${inv.json?.error || inv.json?.note || 'no task_id'}` }; | ||
| const taskId = inv.json.task_id; | ||
| const start = Date.now(); | ||
| let intervalMs = 2500; | ||
| while (Date.now() - start < maxWaitMs) { | ||
| await sleep(intervalMs); | ||
| const poll = await getJson(`${FD_API}/v1/agents/${SLUG}/result/${encodeURIComponent(taskId)}`); | ||
| const d = poll.json || {}; | ||
| const status = d.status || d.outcome; | ||
| if (status === 'completed' || status === 'succeeded' || d.ok === true) { | ||
| return { | ||
| status: 'completed', agent: SLUG, task_id: taskId, output: d.output, charged_pence: d.charged_pence, | ||
| proof_id: d.proof_id || taskId, | ||
| verify_proof_hint: `Verify trustlessly: call forcedream_verify_proof with task_id "${d.proof_id || taskId}". The signature proves authenticity without trusting ForceDream.`, | ||
| message: `Completed. Charged ${d.charged_pence}p. Cryptographically proven (proof_id ${d.proof_id || taskId}).`, | ||
| }; | ||
| } | ||
| if (status === 'charge_failed') | ||
| return { status: 'charge_failed', agent: SLUG, task_id: taskId, charged_pence: 0, message: `Charge failed: ${d.reason || 'insufficient_balance'}. Nothing charged or delivered. Top up and retry.` }; | ||
| if (status === 'failed' || status === 'dead_letter') | ||
| return { status: 'error', agent: SLUG, task_id: taskId, message: `Task ${status}: ${d.reason || d.last_error || 'unknown'}` }; | ||
| intervalMs = Math.min(intervalMs + 1000, 6000); | ||
| } | ||
| return { status: 'pending', agent: SLUG, task_id: taskId, message: `Still processing after ${maxWaitMs / 1000}s. Not re-invoked (would double-charge). Poll the result later with this task_id.` }; | ||
| } |
+21
-0
@@ -12,2 +12,3 @@ #!/usr/bin/env node | ||
| import { scoreLead, scoreLeadSchema } from './score_lead.js'; | ||
| import { generateCode, generateCodeSchema } from './generate_code.js'; | ||
| import { searchReliability, searchReliabilitySchema } from './search_reliability.js'; | ||
@@ -138,2 +139,22 @@ import { searchCosts, searchCostsSchema } from './search_costs.js'; | ||
| }); | ||
| // forcedream_generate_code — dedicated, named tool for code-generation-v1 specifically. | ||
| // Spends balance (needs FD_API_KEY). Same real invoke/poll pattern as the other dedicated | ||
| // tools, fixed to this one agent so a caller doesn't need to know the generic agent_slug pattern. | ||
| server.registerTool('forcedream_generate_code', { | ||
| title: 'Generate verified code', | ||
| description: 'Generates real, working code with real, live verification — not just an LLM\'s opinion. Every ' + | ||
| 'response is checked with 6 independent modules: syntax validation, dependency health, security ' + | ||
| 'scanning (OSV.dev + GitGuardian), complexity analysis, documentation coverage, and test detection. ' + | ||
| 'Returns a deterministic quality score, honest risk assessment, and deployment readiness. SPENDS ' + | ||
| 'your balance — requires FD_API_KEY.', | ||
| inputSchema: generateCodeSchema, | ||
| }, async ({ task_description, max_wait_seconds }) => { | ||
| try { | ||
| const result = await generateCode({ task_description, max_wait_seconds }); | ||
| return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; | ||
| } | ||
| catch (e) { | ||
| return { content: [{ type: 'text', text: JSON.stringify({ status: 'error', error: e.message }, null, 2) }], isError: true }; | ||
| } | ||
| }); | ||
| // forcedream_check_fraud — real, direct, synchronous call (no polling). Spends balance | ||
@@ -140,0 +161,0 @@ // (needs FD_API_KEY). Uses the new, dedicated fd_live_-accepting backend endpoint. |
+1
-1
| { | ||
| "name": "@forcedream/mcp-server", | ||
| "version": "0.9.1", | ||
| "version": "0.10.0", | ||
| "mcpName": "io.github.forcedreamai/mcp-server", | ||
@@ -5,0 +5,0 @@ "description": "Discover, invoke, and trustlessly verify ForceDream AI agents with cryptographic proofs. ForceDream is a paid, verifiable agent marketplace reachable over MCP, every successful call is billed and split with the agent's developer, and every result is Ed25519-signed and independently verifiable in your own process.", |
84048
9.18%19
5.56%1178
10.82%35
12.9%16
14.29%