roam-research-mcp 1.0.0 → 1.3.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.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Simple in-memory cache for page title -> UID mappings.
3
+ * Pages are stable entities that rarely get deleted, making them safe to cache.
4
+ * This reduces redundant API queries when looking up the same page multiple times.
5
+ */
6
+ class PageUidCache {
7
+ constructor() {
8
+ this.cache = new Map(); // title (lowercase) -> UID
9
+ }
10
+ /**
11
+ * Get a cached page UID by title.
12
+ * @param title - Page title (case-insensitive)
13
+ * @returns The cached UID or undefined if not cached
14
+ */
15
+ get(title) {
16
+ return this.cache.get(title.toLowerCase());
17
+ }
18
+ /**
19
+ * Cache a page title -> UID mapping.
20
+ * @param title - Page title (will be stored lowercase)
21
+ * @param uid - Page UID
22
+ */
23
+ set(title, uid) {
24
+ this.cache.set(title.toLowerCase(), uid);
25
+ }
26
+ /**
27
+ * Check if a page title is cached.
28
+ * @param title - Page title (case-insensitive)
29
+ */
30
+ has(title) {
31
+ return this.cache.has(title.toLowerCase());
32
+ }
33
+ /**
34
+ * Called when a page is created - immediately add to cache.
35
+ * @param title - Page title
36
+ * @param uid - Page UID
37
+ */
38
+ onPageCreated(title, uid) {
39
+ this.set(title, uid);
40
+ }
41
+ /**
42
+ * Clear the cache (useful for testing or session reset).
43
+ */
44
+ clear() {
45
+ this.cache.clear();
46
+ }
47
+ /**
48
+ * Get the current cache size.
49
+ */
50
+ get size() {
51
+ return this.cache.size;
52
+ }
53
+ }
54
+ // Singleton instance - shared across all operations
55
+ export const pageUidCache = new PageUidCache();
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env node
2
+ import { initializeGraph } from '@roam-research/roam-api-sdk';
3
+ import { API_TOKEN, GRAPH_NAME } from '../config/environment.js';
4
+ import { PageOperations } from '../tools/operations/pages.js';
5
+ import { parseMarkdown } from '../markdown-utils.js';
6
+ /**
7
+ * Flatten nested MarkdownNode[] to flat array with absolute levels
8
+ */
9
+ function flattenNodes(nodes, baseLevel = 1) {
10
+ const result = [];
11
+ for (const node of nodes) {
12
+ result.push({
13
+ text: node.content,
14
+ level: baseLevel,
15
+ ...(node.heading_level && { heading: node.heading_level })
16
+ });
17
+ if (node.children.length > 0) {
18
+ result.push(...flattenNodes(node.children, baseLevel + 1));
19
+ }
20
+ }
21
+ return result;
22
+ }
23
+ /**
24
+ * Read all input from stdin
25
+ */
26
+ async function readStdin() {
27
+ const chunks = [];
28
+ for await (const chunk of process.stdin) {
29
+ chunks.push(chunk);
30
+ }
31
+ return Buffer.concat(chunks).toString('utf-8');
32
+ }
33
+ /**
34
+ * Show usage help
35
+ */
36
+ function showUsage() {
37
+ console.error('Usage: roam-import <page-title>');
38
+ console.error('');
39
+ console.error('Reads markdown from stdin and imports to Roam Research.');
40
+ console.error('');
41
+ console.error('Examples:');
42
+ console.error(' cat document.md | roam-import "Meeting Notes"');
43
+ console.error(' pbpaste | roam-import "Ideas"');
44
+ console.error(' echo "- Item 1\\n- Item 2" | roam-import "Quick Note"');
45
+ console.error('');
46
+ console.error('Environment variables required:');
47
+ console.error(' ROAM_API_TOKEN Your Roam Research API token');
48
+ console.error(' ROAM_GRAPH_NAME Your Roam graph name');
49
+ }
50
+ async function main() {
51
+ // Parse CLI arguments
52
+ const args = process.argv.slice(2);
53
+ const pageTitle = args[0];
54
+ if (!pageTitle || pageTitle === '--help' || pageTitle === '-h') {
55
+ showUsage();
56
+ process.exit(pageTitle ? 0 : 1);
57
+ }
58
+ // Check if stdin is a TTY (no input piped)
59
+ if (process.stdin.isTTY) {
60
+ console.error('Error: No input received. Pipe markdown content to this command.');
61
+ console.error('');
62
+ showUsage();
63
+ process.exit(1);
64
+ }
65
+ // Read markdown from stdin
66
+ const markdownContent = await readStdin();
67
+ if (!markdownContent.trim()) {
68
+ console.error('Error: Empty input received.');
69
+ process.exit(1);
70
+ }
71
+ // Initialize Roam graph
72
+ const graph = initializeGraph({
73
+ token: API_TOKEN,
74
+ graph: GRAPH_NAME
75
+ });
76
+ // Parse markdown to nodes
77
+ const nodes = parseMarkdown(markdownContent);
78
+ // Flatten nested structure to content blocks
79
+ const contentBlocks = flattenNodes(nodes);
80
+ if (contentBlocks.length === 0) {
81
+ console.error('Error: No content blocks parsed from input.');
82
+ process.exit(1);
83
+ }
84
+ // Create page with content
85
+ const pageOps = new PageOperations(graph);
86
+ const result = await pageOps.createPage(pageTitle, contentBlocks);
87
+ if (result.success) {
88
+ console.log(`Created page '${pageTitle}' (uid: ${result.uid})`);
89
+ }
90
+ else {
91
+ console.error(`Failed to create page '${pageTitle}'`);
92
+ process.exit(1);
93
+ }
94
+ }
95
+ main().catch((error) => {
96
+ console.error(`Error: ${error.message}`);
97
+ process.exit(1);
98
+ });
@@ -42,5 +42,7 @@ if (!API_TOKEN || !GRAPH_NAME) {
42
42
  ' ROAM_GRAPH_NAME=your-graph-name');
43
43
  }
44
44
  const HTTP_STREAM_PORT = process.env.HTTP_STREAM_PORT || '8088'; // Default to 8088
45
- const CORS_ORIGIN = process.env.CORS_ORIGIN || 'http://localhost:5678';
46
- export { API_TOKEN, GRAPH_NAME, HTTP_STREAM_PORT, CORS_ORIGIN };
45
+ const CORS_ORIGINS = (process.env.CORS_ORIGIN || 'http://localhost:5678,https://roamresearch.com')
46
+ .split(',')
47
+ .map(origin => origin.trim());
48
+ export { API_TOKEN, GRAPH_NAME, HTTP_STREAM_PORT, CORS_ORIGINS };
@@ -92,6 +92,51 @@ function parseMarkdown(markdown) {
92
92
  processedLines.push(line);
93
93
  }
94
94
  }
95
+ // First pass: collect all unique indentation values to build level mapping
96
+ const indentationSet = new Set();
97
+ indentationSet.add(0); // Always include level 0
98
+ let inCodeBlockFirstPass = false;
99
+ for (const line of processedLines) {
100
+ const trimmedLine = line.trimEnd();
101
+ if (trimmedLine.match(/^(\s*)```/)) {
102
+ inCodeBlockFirstPass = !inCodeBlockFirstPass;
103
+ if (!inCodeBlockFirstPass)
104
+ continue; // Skip closing ```
105
+ const indent = line.match(/^\s*/)?.[0].length ?? 0;
106
+ indentationSet.add(indent);
107
+ continue;
108
+ }
109
+ if (inCodeBlockFirstPass || trimmedLine === '')
110
+ continue;
111
+ const bulletMatch = trimmedLine.match(/^(\s*)[-*+]\s+/);
112
+ if (bulletMatch) {
113
+ indentationSet.add(bulletMatch[1].length);
114
+ }
115
+ else {
116
+ const indent = line.match(/^\s*/)?.[0].length ?? 0;
117
+ indentationSet.add(indent);
118
+ }
119
+ }
120
+ // Create sorted array of indentation values and map to sequential levels
121
+ const sortedIndents = Array.from(indentationSet).sort((a, b) => a - b);
122
+ const indentToLevel = new Map();
123
+ sortedIndents.forEach((indent, index) => {
124
+ indentToLevel.set(indent, index);
125
+ });
126
+ // Helper to get level from indentation, finding closest match
127
+ function getLevel(indent) {
128
+ if (indentToLevel.has(indent)) {
129
+ return indentToLevel.get(indent);
130
+ }
131
+ // Find the closest smaller indentation
132
+ let closestLevel = 0;
133
+ for (const [ind, lvl] of indentToLevel) {
134
+ if (ind <= indent && lvl > closestLevel) {
135
+ closestLevel = lvl;
136
+ }
137
+ }
138
+ return closestLevel;
139
+ }
95
140
  const rootNodes = [];
96
141
  const stack = [];
97
142
  let inCodeBlock = false;
@@ -133,7 +178,7 @@ function parseMarkdown(markdown) {
133
178
  }
134
179
  return codeLine.trimStart();
135
180
  });
136
- const level = Math.floor(codeBlockIndentation / 2);
181
+ const level = getLevel(codeBlockIndentation);
137
182
  const node = {
138
183
  content: processedCodeLines.join('\n'),
139
184
  level,
@@ -169,17 +214,18 @@ function parseMarkdown(markdown) {
169
214
  if (trimmedLine === '') {
170
215
  continue;
171
216
  }
172
- const indentation = line.match(/^\s*/)?.[0].length ?? 0;
173
- let level = Math.floor(indentation / 2);
217
+ let indentation;
174
218
  let contentToParse;
175
219
  const bulletMatch = trimmedLine.match(/^(\s*)[-*+]\s+/);
176
220
  if (bulletMatch) {
177
- level = Math.floor(bulletMatch[1].length / 2);
221
+ indentation = bulletMatch[1].length;
178
222
  contentToParse = trimmedLine.substring(bulletMatch[0].length);
179
223
  }
180
224
  else {
225
+ indentation = line.match(/^\s*/)?.[0].length ?? 0;
181
226
  contentToParse = trimmedLine;
182
227
  }
228
+ const level = getLevel(indentation);
183
229
  const { heading_level, content: finalContent } = parseMarkdownHeadingLevel(contentToParse);
184
230
  const node = {
185
231
  content: finalContent,
@@ -240,7 +286,7 @@ function parseTableRows(lines) {
240
286
  }
241
287
  return tableNodes;
242
288
  }
243
- function generateBlockUid() {
289
+ export function generateBlockUid() {
244
290
  // Generate a random string of 9 characters (Roam's format) using crypto for better randomness
245
291
  const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
246
292
  // 64 chars, which divides 256 evenly (256 = 64 * 4), so simple modulo is unbiased
@@ -11,7 +11,7 @@ import { join, dirname } from 'node:path';
11
11
  import { createServer } from 'node:http';
12
12
  import { fileURLToPath } from 'node:url';
13
13
  import { findAvailablePort } from '../utils/net.js';
14
- import { CORS_ORIGIN } from '../config/environment.js';
14
+ import { CORS_ORIGINS } from '../config/environment.js';
15
15
  const __filename = fileURLToPath(import.meta.url);
16
16
  const __dirname = dirname(__filename);
17
17
  // Read package.json to get the version
@@ -211,6 +211,18 @@ export class RoamServer {
211
211
  content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
212
212
  };
213
213
  }
214
+ case 'roam_create_table': {
215
+ const { parent_uid, order, headers, rows } = request.params.arguments;
216
+ const result = await this.toolHandlers.createTable({
217
+ parent_uid,
218
+ order,
219
+ headers,
220
+ rows
221
+ });
222
+ return {
223
+ content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
224
+ };
225
+ }
214
226
  default:
215
227
  throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`);
216
228
  }
@@ -229,23 +241,70 @@ export class RoamServer {
229
241
  const stdioMcpServer = this.createMcpServer();
230
242
  const stdioTransport = new StdioServerTransport();
231
243
  await stdioMcpServer.connect(stdioTransport);
232
- const httpMcpServer = this.createMcpServer('-http');
233
- const httpStreamTransport = new StreamableHTTPServerTransport({
234
- sessionIdGenerator: () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15),
235
- });
236
- await httpMcpServer.connect(httpStreamTransport);
244
+ // Track active transports by session ID for proper session management
245
+ const activeSessions = new Map();
237
246
  const httpServer = createServer(async (req, res) => {
238
- // Set CORS headers
239
- res.setHeader('Access-Control-Allow-Origin', CORS_ORIGIN);
240
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
241
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
247
+ // Set CORS headers dynamically based on request origin
248
+ const requestOrigin = req.headers.origin;
249
+ if (requestOrigin && CORS_ORIGINS.includes(requestOrigin)) {
250
+ res.setHeader('Access-Control-Allow-Origin', requestOrigin);
251
+ }
252
+ else if (CORS_ORIGINS.includes('*')) {
253
+ res.setHeader('Access-Control-Allow-Origin', '*');
254
+ }
255
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
256
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Mcp-Session-Id');
257
+ res.setHeader('Access-Control-Expose-Headers', 'Mcp-Session-Id');
258
+ res.setHeader('Access-Control-Allow-Credentials', 'true');
242
259
  // Handle preflight OPTIONS requests
243
260
  if (req.method === 'OPTIONS') {
244
261
  res.writeHead(204); // No Content
245
262
  res.end();
246
263
  return;
247
264
  }
265
+ // Check for existing session ID in header
266
+ const sessionId = req.headers['mcp-session-id'];
267
+ // Handle session termination (DELETE request)
268
+ if (req.method === 'DELETE' && sessionId) {
269
+ const transport = activeSessions.get(sessionId);
270
+ if (transport) {
271
+ await transport.close();
272
+ activeSessions.delete(sessionId);
273
+ res.writeHead(200);
274
+ res.end();
275
+ }
276
+ else {
277
+ res.writeHead(404, { 'Content-Type': 'application/json' });
278
+ res.end(JSON.stringify({ error: 'Session not found' }));
279
+ }
280
+ return;
281
+ }
248
282
  try {
283
+ // If we have an existing session, use that transport
284
+ if (sessionId && activeSessions.has(sessionId)) {
285
+ const transport = activeSessions.get(sessionId);
286
+ await transport.handleRequest(req, res);
287
+ return;
288
+ }
289
+ // Create new transport and server for new sessions
290
+ const httpMcpServer = this.createMcpServer('-http');
291
+ const httpStreamTransport = new StreamableHTTPServerTransport({
292
+ sessionIdGenerator: () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15),
293
+ onsessioninitialized: (newSessionId) => {
294
+ activeSessions.set(newSessionId, httpStreamTransport);
295
+ }
296
+ });
297
+ // Clean up session when transport closes
298
+ httpStreamTransport.onclose = () => {
299
+ const entries = activeSessions.entries();
300
+ for (const [key, value] of entries) {
301
+ if (value === httpStreamTransport) {
302
+ activeSessions.delete(key);
303
+ break;
304
+ }
305
+ }
306
+ };
307
+ await httpMcpServer.connect(httpStreamTransport);
249
308
  await httpStreamTransport.handleRequest(req, res);
250
309
  }
251
310
  catch (error) {
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Structured error types for the Roam MCP server.
3
+ * Provides consistent error handling across all tools.
4
+ */
5
+ /**
6
+ * Creates a structured validation error response.
7
+ */
8
+ export function createValidationError(message, details, recovery) {
9
+ return {
10
+ code: 'VALIDATION_ERROR',
11
+ message,
12
+ details,
13
+ recovery
14
+ };
15
+ }
16
+ /**
17
+ * Creates a structured rate limit error response.
18
+ */
19
+ export function createRateLimitError(retryAfterMs) {
20
+ return {
21
+ code: 'RATE_LIMIT',
22
+ message: 'Too many requests, please retry after backoff',
23
+ recovery: {
24
+ retry_after_ms: retryAfterMs ?? 60000,
25
+ suggestion: 'Wait for the specified duration before retrying'
26
+ }
27
+ };
28
+ }
29
+ /**
30
+ * Creates a structured API error response.
31
+ */
32
+ export function createApiError(message, details) {
33
+ return {
34
+ code: 'API_ERROR',
35
+ message,
36
+ details
37
+ };
38
+ }
39
+ /**
40
+ * Creates a structured transaction failed error response.
41
+ */
42
+ export function createTransactionFailedError(message, failedAtAction, committed) {
43
+ return {
44
+ success: false,
45
+ error: {
46
+ code: 'TRANSACTION_FAILED',
47
+ message,
48
+ details: failedAtAction !== undefined ? { action_index: failedAtAction } : undefined
49
+ },
50
+ committed
51
+ };
52
+ }
53
+ /**
54
+ * Checks if an error is a rate limit error based on error message.
55
+ */
56
+ export function isRateLimitError(error) {
57
+ if (error instanceof Error) {
58
+ const message = error.message.toLowerCase();
59
+ return message.includes('too many requests') ||
60
+ message.includes('rate limit') ||
61
+ message.includes('try again in');
62
+ }
63
+ if (typeof error === 'string') {
64
+ const message = error.toLowerCase();
65
+ return message.includes('too many requests') ||
66
+ message.includes('rate limit') ||
67
+ message.includes('try again in');
68
+ }
69
+ return false;
70
+ }
71
+ /**
72
+ * Checks if an error is a network error.
73
+ */
74
+ export function isNetworkError(error) {
75
+ if (error instanceof Error) {
76
+ const message = error.message.toLowerCase();
77
+ return message.includes('network') ||
78
+ message.includes('econnrefused') ||
79
+ message.includes('econnreset') ||
80
+ message.includes('etimedout') ||
81
+ message.includes('socket');
82
+ }
83
+ return false;
84
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Shared utilities for the Roam MCP server.
3
+ */
4
+ export * from './validation.js';
5
+ export * from './errors.js';