Branchwell

Jotley / app · jotley.ai / macOS / Resources / JotleyClaudeMCP / server.js

1'use strict';
2
3// Claude Desktop supplies Node. No package installation, shell command or
4// direct access to the note database is needed. Jotley enforces every request.
5const http = require('node:http');
6const readline = require('node:readline');
7const maxRequestBytes = 1024 * 1024;
8const maxResponseBytes = 4 * 1024 * 1024;
9const pending = new Map();
10let protocolVersion = '2025-11-25';
11let endpoint;
12let token = process.env.JOTLEY_MCP_TOKEN || '';
13
14function respond(message) {
15 process.stdout.write(JSON.stringify(message) + '\n');
16}
17
18function failure(id, message, code = -32000) {
19 respond({ jsonrpc: '2.0', id, error: { code, message } });
20}
21
22try {
23 endpoint = new URL(process.env.JOTLEY_MCP_URL || 'http://127.0.0.1:17321/mcp');
24 if (endpoint.protocol !== 'http:' || endpoint.hostname !== '127.0.0.1' || !endpoint.port ||
25 endpoint.pathname !== '/mcp' || endpoint.username || endpoint.password || endpoint.search || endpoint.hash) {
26 throw new Error('Invalid Jotley address');
27 }
28 if (!/^[a-f0-9]{64}$/i.test(token)) throw new Error('Missing Jotley token');
29} catch {
30 process.stderr.write('Open the Jotley extension settings and paste the access token from Jotley Settings → Integrations → MCP.\n');
31 process.exit(1);
32}
33
34const input = readline.createInterface({ input: process.stdin, terminal: false });
35input.on('line', line => {
36 if (!line.trim()) return;
37 if (Buffer.byteLength(line, 'utf8') > maxRequestBytes) {
38 failure(null, 'This request is too large for Jotley.', -32600);
39 return;
40 }
41 let message;
42 try { message = JSON.parse(line); }
43 catch { failure(null, 'Invalid JSON request.', -32700); return; }
44 if (!message || Array.isArray(message) || message.jsonrpc !== '2.0' || typeof message.method !== 'string') {
45 failure(null, 'Invalid MCP request.', -32600);
46 return;
47 }
48 const hasID = Object.hasOwn(message, 'id');
49 if (hasID && typeof message.id !== 'string' && typeof message.id !== 'number') {
50 failure(null, 'Invalid request identifier.', -32600);
51 return;
52 }
53 if (message.method === 'notifications/cancelled') {
54 const active = pending.get(message.params?.requestId);
55 if (active) active.destroy(new Error('Cancelled'));
56 return;
57 }
58 if (pending.size >= 32) {
59 if (hasID) failure(message.id, 'Jotley is handling other requests. Try again shortly.');
60 return;
61 }
62 const body = Buffer.from(JSON.stringify(message), 'utf8');
63 const request = http.request(endpoint, {
64 method: 'POST',
65 headers: {
66 'Content-Type': 'application/json',
67 Accept: 'application/json, text/event-stream',
68 Authorization: 'Bearer ' + token,
69 'MCP-Protocol-Version': protocolVersion,
70 'Content-Length': body.length
71 }
72 }, response => {
73 const chunks = [];
74 let bytes = 0;
75 response.on('data', chunk => {
76 bytes += chunk.length;
77 if (bytes > maxResponseBytes) { request.destroy(new Error('Response too large')); return; }
78 chunks.push(chunk);
79 });
80 response.on('end', () => {
81 if (!hasID) return;
82 if (response.statusCode === 401 || response.statusCode === 403) {
83 failure(message.id, 'Jotley access needs reconnecting. Copy the current token from Jotley Settings → Integrations → MCP into this extension’s settings.');
84 return;
85 }
86 try {
87 const result = JSON.parse(Buffer.concat(chunks).toString('utf8'));
88 if (!result || result.jsonrpc !== '2.0' || result.id !== message.id) throw new Error('Invalid response');
89 if (message.method === 'initialize' && result.result?.protocolVersion) protocolVersion = result.result.protocolVersion;
90 respond(result);
91 } catch { failure(message.id, 'Jotley returned an unavailable response. Keep Jotley open with MCP access enabled and try again.'); }
92 });
93 response.on('error', () => request.destroy(new Error('Response interrupted')));
94 });
95 if (hasID) pending.set(message.id, request);
96 request.setTimeout(600000, () => request.destroy(new Error('Timed out')));
97 request.on('error', () => {
98 if (hasID) failure(message.id, 'Open Jotley and enable MCP access in Settings → Integrations → MCP, then retry.');
99 });
100 request.on('close', () => { if (hasID && pending.get(message.id) === request) pending.delete(message.id); });
101 request.end(body);
102});
103input.on('close', () => {
104 for (const request of pending.values()) request.destroy();
105 token = '';
106});