crispparse-mcp 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +88 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,11 +1,17 @@
1
1
  #!/usr/bin/env node
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
2
5
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
6
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
7
  import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
5
8
 
6
9
  const API_URL = process.env.CRISPPARSE_API_URL || "https://api.crispparse.com/v1/extract";
10
+ const TRIAL_URL = process.env.CRISPPARSE_TRIAL_URL || API_URL.replace(/\/v1\/extract\/?$/, "/v1/trial/claim");
7
11
  const DEFAULT_API_KEY = process.env.CRISPPARSE_API_KEY || "";
8
12
  const DEFAULT_PAYMENT_SIGNATURE = process.env.CRISPPARSE_PAYMENT_SIGNATURE || "";
13
+ const AUTO_CLAIM_TRIAL = process.env.CRISPPARSE_AUTO_CLAIM_TRIAL !== "false";
14
+ const CACHE_FILE = path.join(os.homedir(), ".crispparse", "trial_token.json");
9
15
 
10
16
  function safeJson(text) {
11
17
  try {
@@ -15,8 +21,47 @@ function safeJson(text) {
15
21
  }
16
22
  }
17
23
 
24
+ function readCachedTrialToken() {
25
+ try {
26
+ const raw = fs.readFileSync(CACHE_FILE, "utf8");
27
+ const parsed = JSON.parse(raw);
28
+ if (parsed && typeof parsed.token === "string" && parsed.token.length > 0) {
29
+ return parsed.token;
30
+ }
31
+ } catch {}
32
+ return "";
33
+ }
34
+
35
+ function writeCachedTrialToken(token) {
36
+ try {
37
+ fs.mkdirSync(path.dirname(CACHE_FILE), { recursive: true, mode: 0o700 });
38
+ fs.writeFileSync(CACHE_FILE, JSON.stringify({ token, claimed_at: new Date().toISOString() }, null, 2), { mode: 0o600 });
39
+ } catch {}
40
+ }
41
+
42
+ function clearCachedTrialToken() {
43
+ try {
44
+ fs.unlinkSync(CACHE_FILE);
45
+ } catch {}
46
+ }
47
+
48
+ async function claimTrialToken(forceRefresh = false) {
49
+ if (!forceRefresh) {
50
+ const cached = readCachedTrialToken();
51
+ if (cached) return { token: cached, source: "cache" };
52
+ }
53
+ const res = await fetch(TRIAL_URL, { method: "POST" });
54
+ const text = await res.text();
55
+ const data = safeJson(text);
56
+ if (!res.ok || !data || typeof data.macaroon_token !== "string") {
57
+ throw new Error(typeof data === "object" && data?.detail ? data.detail : `Trial claim failed (${res.status}): ${text}`);
58
+ }
59
+ writeCachedTrialToken(data.macaroon_token);
60
+ return { token: data.macaroon_token, source: "claimed", details: data };
61
+ }
62
+
18
63
  const server = new Server(
19
- { name: "crispparse-mcp-server", version: "1.0.1" },
64
+ { name: "crispparse-mcp-server", version: "1.0.2" },
20
65
  { capabilities: { tools: {} } }
21
66
  );
22
67
 
@@ -24,7 +69,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => ({
24
69
  tools: [
25
70
  {
26
71
  name: "crispparse_extract",
27
- description: "Extract clean, LLM-ready Markdown or structured JSON from any public URL using CrispParse (supports Bearer Macaroon or x402 PAYMENT-SIGNATURE Base USDC proof).",
72
+ description: "Extract clean, LLM-ready Markdown or structured JSON from any public URL using CrispParse (auto-provisions a free 50-credit trial Macaroon if no API key or x402 PAYMENT-SIGNATURE is configured).",
28
73
  inputSchema: {
29
74
  type: "object",
30
75
  properties: {
@@ -35,24 +80,60 @@ server.setRequestHandler(ListToolsRequestSchema, async () => ({
35
80
  },
36
81
  required: ["url"]
37
82
  }
83
+ },
84
+ {
85
+ name: "crispparse_claim_trial",
86
+ description: "Claim or retrieve a cached zero-config CrispParse trial Macaroon token (50 credits, 14-day TTL) for immediate extraction without manual signup.",
87
+ inputSchema: {
88
+ type: "object",
89
+ properties: {
90
+ force_refresh: { type: "boolean", description: "Request a fresh trial token instead of returning the cached token." }
91
+ }
92
+ }
38
93
  }
39
94
  ]
40
95
  }));
41
96
 
42
97
  server.setRequestHandler(CallToolRequestSchema, async ({ params }) => {
98
+ if (params.name === "crispparse_claim_trial") {
99
+ try {
100
+ const result = await claimTrialToken(Boolean(params.arguments?.force_refresh));
101
+ return {
102
+ isError: false,
103
+ content: [{ type: "text", text: JSON.stringify({ status: "ok", source: result.source, macaroon_token: result.token, cache_path: CACHE_FILE }) }]
104
+ };
105
+ } catch (error) {
106
+ return {
107
+ isError: true,
108
+ content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }]
109
+ };
110
+ }
111
+ }
112
+
43
113
  if (params.name !== "crispparse_extract") {
44
114
  return { isError: true, content: [{ type: "text", text: `Unknown tool: ${params.name}` }] };
45
115
  }
46
116
 
47
117
  const targetUrl = params.arguments?.url;
48
118
  const mode = params.arguments?.mode === "json" ? "json" : "markdown";
49
- const apiKey = (typeof params.arguments?.api_key === "string" && params.arguments.api_key) || DEFAULT_API_KEY;
119
+ let apiKey = (typeof params.arguments?.api_key === "string" && params.arguments.api_key) || DEFAULT_API_KEY;
50
120
  const paymentSignature = (typeof params.arguments?.payment_signature === "string" && params.arguments.payment_signature) || DEFAULT_PAYMENT_SIGNATURE;
51
121
 
52
122
  if (!targetUrl || typeof targetUrl !== "string") {
53
123
  return { isError: true, content: [{ type: "text", text: "Missing required parameter: url" }] };
54
124
  }
55
125
 
126
+ let usedTrialCache = false;
127
+ if (!apiKey && !paymentSignature && AUTO_CLAIM_TRIAL) {
128
+ try {
129
+ const trial = await claimTrialToken(false);
130
+ apiKey = trial.token;
131
+ usedTrialCache = true;
132
+ } catch {
133
+ // Fall through to unauthenticated request so the agent still receives the x402 402 payment challenge
134
+ }
135
+ }
136
+
56
137
  try {
57
138
  const headers = {
58
139
  "Content-Type": "application/json",
@@ -68,6 +149,10 @@ server.setRequestHandler(CallToolRequestSchema, async ({ params }) => {
68
149
 
69
150
  const bodyText = await response.text();
70
151
 
152
+ if ((response.status === 401 || response.status === 402) && usedTrialCache) {
153
+ clearCachedTrialToken();
154
+ }
155
+
71
156
  if (response.status === 402) {
72
157
  return {
73
158
  isError: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crispparse-mcp",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Local stdio Model Context Protocol (MCP) server for deterministic web extraction to clean Markdown and JSON via CrispParse.",
5
5
  "type": "module",
6
6
  "main": "index.js",