@workover/mcp 0.1.0 → 0.2.1
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.
- package/index.mjs +52 -68
- package/lib.mjs +177 -0
- package/package.json +5 -1
package/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Workover MCP server (Phase 2 — read tools). Lets Claude Code / any MCP client read the signed-in
|
|
3
3
|
// user's Workover documents. Auth = the token stored by `workover login` (~/.workover/config.json).
|
|
4
4
|
// Read-only for now; propose-edits (as suggestions) lands in Phase 3. Never publishes to WordPress.
|
|
5
|
+
// The api caller + tool dispatch live in lib.mjs (injectable for tests); this file just wires reality.
|
|
5
6
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
7
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
7
8
|
import {
|
|
@@ -11,36 +12,20 @@ import {
|
|
|
11
12
|
import { readFileSync } from 'fs';
|
|
12
13
|
import { homedir } from 'os';
|
|
13
14
|
import { join } from 'path';
|
|
15
|
+
import { makeApi, makeDispatch, validAuth } from './lib.mjs';
|
|
14
16
|
|
|
15
17
|
function loadAuth() {
|
|
16
18
|
try {
|
|
17
19
|
const cfg = JSON.parse(readFileSync(join(homedir(), '.workover', 'config.json'), 'utf8'));
|
|
18
|
-
|
|
20
|
+
return validAuth(cfg);
|
|
19
21
|
} catch {
|
|
20
|
-
|
|
22
|
+
return null; // not signed in
|
|
21
23
|
}
|
|
22
|
-
return null;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
const auth = loadAuth();
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const res = await fetch(`${auth.api}${path}`, {
|
|
29
|
-
...init,
|
|
30
|
-
headers: { authorization: `Bearer ${auth.token}`, ...(init?.headers || {}) },
|
|
31
|
-
});
|
|
32
|
-
if (res.status === 401) throw new Error('Your Workover login expired or was revoked. Run `workover login`.');
|
|
33
|
-
if (res.status === 403) throw new Error('This token isn’t allowed to do that.');
|
|
34
|
-
if (!res.ok) throw new Error(`Workover API error (${res.status}).`);
|
|
35
|
-
return res.json();
|
|
36
|
-
}
|
|
37
|
-
const apiPost = (path, body) =>
|
|
38
|
-
api(path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
|
|
39
|
-
|
|
40
|
-
const stripHtml = (h) =>
|
|
41
|
-
(h || '').replace(/<[^>]+>/g, ' ').replace(/&[a-z]+;/g, ' ').replace(/\s+/g, ' ').trim();
|
|
42
|
-
const text = (t) => ({ content: [{ type: 'text', text: t }] });
|
|
43
|
-
const err = (t) => ({ content: [{ type: 'text', text: t }], isError: true });
|
|
27
|
+
const { api, apiPost } = makeApi({ auth: auth ?? { api: '', token: '' } });
|
|
28
|
+
const dispatch = makeDispatch({ auth, api, apiPost, readFile: readFileSync });
|
|
44
29
|
|
|
45
30
|
const TOOLS = [
|
|
46
31
|
{
|
|
@@ -58,7 +43,11 @@ const TOOLS = [
|
|
|
58
43
|
description: 'Read the full text of a Workover document by its id.',
|
|
59
44
|
inputSchema: {
|
|
60
45
|
type: 'object',
|
|
61
|
-
properties: { docId: {
|
|
46
|
+
properties: { docId: {
|
|
47
|
+
type: 'number',
|
|
48
|
+
description:
|
|
49
|
+
'The numeric document id, as printed by workover_search_docs ("#12345" → 12345). Not the handle from a document URL.',
|
|
50
|
+
} },
|
|
62
51
|
required: ['docId'],
|
|
63
52
|
},
|
|
64
53
|
},
|
|
@@ -67,6 +56,41 @@ const TOOLS = [
|
|
|
67
56
|
description: 'List the projects/workspaces you can access in Workover.',
|
|
68
57
|
inputSchema: { type: 'object', properties: {} },
|
|
69
58
|
},
|
|
59
|
+
{
|
|
60
|
+
name: 'workover_create_doc',
|
|
61
|
+
description:
|
|
62
|
+
"Create a new document in a Workover project (find the projectId with workover_list_projects). The body is added as a proposal, so it appears as tracked-change suggestions for the person to review and accept when they open the doc — nothing is published. Returns the new document's id and URL.",
|
|
63
|
+
inputSchema: {
|
|
64
|
+
type: 'object',
|
|
65
|
+
properties: {
|
|
66
|
+
projectId: {
|
|
67
|
+
type: 'number',
|
|
68
|
+
description:
|
|
69
|
+
'The numeric id of the project to create the document in, as printed by workover_list_projects ("#7" → 7).',
|
|
70
|
+
},
|
|
71
|
+
title: { type: 'string', description: 'The document title.' },
|
|
72
|
+
markdown: { type: 'string', description: 'The document body as markdown.' },
|
|
73
|
+
},
|
|
74
|
+
required: ['projectId', 'title', 'markdown'],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'workover_upload_image',
|
|
79
|
+
description:
|
|
80
|
+
"Upload an image (e.g. a product screenshot you captured) into a Workover document's media, and get back a URL to use in the document markdown as . Provide the local file path.",
|
|
81
|
+
inputSchema: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
docId: {
|
|
85
|
+
type: 'number',
|
|
86
|
+
description: 'The numeric id of the document the image belongs to (not a URL handle).',
|
|
87
|
+
},
|
|
88
|
+
path: { type: 'string', description: 'Local file path to the image (png/jpg/webp).' },
|
|
89
|
+
alt: { type: 'string', description: 'Alt text describing the image.' },
|
|
90
|
+
},
|
|
91
|
+
required: ['docId', 'path'],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
70
94
|
{
|
|
71
95
|
name: 'workover_propose_edit',
|
|
72
96
|
description:
|
|
@@ -74,7 +98,11 @@ const TOOLS = [
|
|
|
74
98
|
inputSchema: {
|
|
75
99
|
type: 'object',
|
|
76
100
|
properties: {
|
|
77
|
-
docId: {
|
|
101
|
+
docId: {
|
|
102
|
+
type: 'number',
|
|
103
|
+
description:
|
|
104
|
+
'The numeric id of the document to propose changes to (not a URL handle).',
|
|
105
|
+
},
|
|
78
106
|
markdown: {
|
|
79
107
|
type: 'string',
|
|
80
108
|
description: 'The complete revised document as markdown (not just the changed parts).',
|
|
@@ -97,52 +125,8 @@ const server = new Server(
|
|
|
97
125
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS }));
|
|
98
126
|
|
|
99
127
|
server.setRequestHandler(CallToolRequestSchema, async (req) => {
|
|
100
|
-
if (!auth)
|
|
101
|
-
return err('Not signed in to Workover. Run `workover login` in your terminal first.');
|
|
102
128
|
const { name, arguments: args = {} } = req.params;
|
|
103
|
-
|
|
104
|
-
if (name === 'workover_search_docs') {
|
|
105
|
-
const { results = [] } = await api(
|
|
106
|
-
`/search/documents?q=${encodeURIComponent(String(args.query || ''))}&limit=10`,
|
|
107
|
-
);
|
|
108
|
-
if (!results.length) return text('No matching documents.');
|
|
109
|
-
return text(
|
|
110
|
-
results
|
|
111
|
-
.map(
|
|
112
|
-
(r) =>
|
|
113
|
-
`#${r.id} — ${r.title || 'Untitled'}${r.snippet ? `\n ${stripHtml(r.snippet)}` : ''}`,
|
|
114
|
-
)
|
|
115
|
-
.join('\n'),
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
if (name === 'workover_read_doc') {
|
|
119
|
-
const d = await api(`/project/doc/${Number(args.docId)}`);
|
|
120
|
-
if (!d || !d.id) return err(`Document ${args.docId} not found (or no access).`);
|
|
121
|
-
const body = d.bodyText || stripHtml(d.content) || '(empty document)';
|
|
122
|
-
return text(`# ${d.title || 'Untitled'} (id ${d.id})\n\n${body}`);
|
|
123
|
-
}
|
|
124
|
-
if (name === 'workover_list_projects') {
|
|
125
|
-
const projects = await api('/project');
|
|
126
|
-
const list = Array.isArray(projects) ? projects : projects?.projects || [];
|
|
127
|
-
if (!list.length) return text('No projects.');
|
|
128
|
-
return text(list.map((p) => `#${p.id} — ${p.name || p.title || 'Project'}`).join('\n'));
|
|
129
|
-
}
|
|
130
|
-
if (name === 'workover_propose_edit') {
|
|
131
|
-
const md = String(args.markdown || '').trim();
|
|
132
|
-
if (!md) return err('Provide the full revised document as `markdown`.');
|
|
133
|
-
const res = await apiPost(`/project/doc/${Number(args.docId)}/propose`, {
|
|
134
|
-
markdown: md,
|
|
135
|
-
note: args.note ? String(args.note) : undefined,
|
|
136
|
-
source: 'Claude Code',
|
|
137
|
-
});
|
|
138
|
-
return text(
|
|
139
|
-
`Proposed your changes to document #${args.docId}. They'll appear as suggestions to accept or reject when the document is open in Workover${res?.documentUrl ? `:\n${res.documentUrl}` : '.'}\n\nNothing is published until the person reviews and accepts.`,
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
return err(`Unknown tool: ${name}`);
|
|
143
|
-
} catch (e) {
|
|
144
|
-
return err(e?.message || 'Something went wrong.');
|
|
145
|
-
}
|
|
129
|
+
return dispatch(name, args);
|
|
146
130
|
});
|
|
147
131
|
|
|
148
132
|
await server.connect(new StdioServerTransport());
|
package/lib.mjs
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Pure/injectable helpers for the Workover MCP server, split out so they can be unit-tested without
|
|
2
|
+
// importing index.mjs (which starts the stdio server on load). index.mjs wires the real fetch/auth/fs
|
|
3
|
+
// into makeApi + makeDispatch; tests inject stubs.
|
|
4
|
+
|
|
5
|
+
/** Flatten HTML to readable plain text: drop tags + named entities, collapse whitespace. */
|
|
6
|
+
export const stripHtml = (h) =>
|
|
7
|
+
(h || '')
|
|
8
|
+
.replace(/<[^>]+>/g, ' ')
|
|
9
|
+
.replace(/&[a-z]+;/g, ' ')
|
|
10
|
+
.replace(/\s+/g, ' ')
|
|
11
|
+
.trim();
|
|
12
|
+
|
|
13
|
+
/** Wrap a string as a successful MCP tool result. */
|
|
14
|
+
export const text = (t) => ({ content: [{ type: 'text', text: t }] });
|
|
15
|
+
|
|
16
|
+
/** Wrap a string as an MCP tool error result (isError: true → the client shows it as a failure). */
|
|
17
|
+
export const err = (t) => ({ content: [{ type: 'text', text: t }], isError: true });
|
|
18
|
+
|
|
19
|
+
/** True only when a stored config has both a token and an api base — the sign-in gate. */
|
|
20
|
+
export const validAuth = (cfg) => (cfg?.token && cfg?.api ? cfg : null);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Coerce a tool-supplied row id, or null if it isn't one.
|
|
24
|
+
*
|
|
25
|
+
* Agents pass whatever they have to hand: the `#12345` shown by workover_search_docs, or the
|
|
26
|
+
* publicId handle out of a document URL. `Number(...)` turned every one of those into `NaN`, which
|
|
27
|
+
* we then sent as a literal `/project/doc/NaN` — the API answered 500 and paged on-call (the same
|
|
28
|
+
* class of bug as Better Stack incident 995716123). Reject it here, with a message that says what
|
|
29
|
+
* to pass instead, rather than making the server decide.
|
|
30
|
+
*/
|
|
31
|
+
export const rowId = (v) => {
|
|
32
|
+
const s = String(v ?? '')
|
|
33
|
+
.trim()
|
|
34
|
+
.replace(/^#/, '');
|
|
35
|
+
return /^\d+$/.test(s) ? Number(s) : null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** The error shown when a tool is handed something that isn't a numeric id. */
|
|
39
|
+
export const badIdErr = (kind, got) =>
|
|
40
|
+
err(
|
|
41
|
+
`"${got}" isn't a ${kind} id. Pass the numeric id (e.g. 12345) — the one workover_search_docs prints as "#12345" or workover_list_projects as "#7". A handle from a document URL won't work here.`,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
/** Map an image file extension to its mime type (defaults to png). */
|
|
45
|
+
export const mimeForExt = (ext) => {
|
|
46
|
+
const e = String(ext || '').toLowerCase();
|
|
47
|
+
if (e === 'jpg' || e === 'jpeg') return 'image/jpeg';
|
|
48
|
+
if (e === 'webp') return 'image/webp';
|
|
49
|
+
return 'image/png';
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Build the authenticated JSON API caller. `fetchImpl` is injectable for tests. The status→message
|
|
54
|
+
* mapping is the contract the tools rely on (401/403 are friendly, other non-2xx is generic).
|
|
55
|
+
*/
|
|
56
|
+
export function makeApi({ auth, fetchImpl = fetch }) {
|
|
57
|
+
async function api(path, init) {
|
|
58
|
+
const res = await fetchImpl(`${auth.api}${path}`, {
|
|
59
|
+
...init,
|
|
60
|
+
headers: { authorization: `Bearer ${auth.token}`, ...(init?.headers || {}) },
|
|
61
|
+
});
|
|
62
|
+
if (res.status === 401)
|
|
63
|
+
throw new Error('Your Workover login expired or was revoked. Run `workover login`.');
|
|
64
|
+
if (res.status === 403) throw new Error('This token isn’t allowed to do that.');
|
|
65
|
+
if (!res.ok) throw new Error(`Workover API error (${res.status}).`);
|
|
66
|
+
return res.json();
|
|
67
|
+
}
|
|
68
|
+
const apiPost = (path, body) =>
|
|
69
|
+
api(path, {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: { 'content-type': 'application/json' },
|
|
72
|
+
body: JSON.stringify(body),
|
|
73
|
+
});
|
|
74
|
+
return { api, apiPost };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Build the CallTool dispatcher. Returns `dispatch(name, args)` → an MCP result. Not-signed-in
|
|
79
|
+
* short-circuits before any request; unknown tools and thrown errors become `err(...)`. `readFile`
|
|
80
|
+
* and `fetchImpl` are injected so the upload path is testable without touching the disk/network.
|
|
81
|
+
*/
|
|
82
|
+
export function makeDispatch({ auth, api, apiPost, fetchImpl = fetch, readFile }) {
|
|
83
|
+
return async function dispatch(name, args = {}) {
|
|
84
|
+
if (!auth)
|
|
85
|
+
return err('Not signed in to Workover. Run `workover login` in your terminal first.');
|
|
86
|
+
try {
|
|
87
|
+
if (name === 'workover_search_docs') {
|
|
88
|
+
const { results = [] } = await api(
|
|
89
|
+
`/search/documents?q=${encodeURIComponent(String(args.query || ''))}&limit=10`,
|
|
90
|
+
);
|
|
91
|
+
if (!results.length) return text('No matching documents.');
|
|
92
|
+
return text(
|
|
93
|
+
results
|
|
94
|
+
.map(
|
|
95
|
+
(r) =>
|
|
96
|
+
`#${r.id} — ${r.title || 'Untitled'}${r.snippet ? `\n ${stripHtml(r.snippet)}` : ''}`,
|
|
97
|
+
)
|
|
98
|
+
.join('\n'),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (name === 'workover_read_doc') {
|
|
102
|
+
const docId = rowId(args.docId);
|
|
103
|
+
if (docId === null) return badIdErr('document', args.docId);
|
|
104
|
+
const d = await api(`/project/doc/${docId}`);
|
|
105
|
+
if (!d || !d.id) return err(`Document ${docId} not found (or no access).`);
|
|
106
|
+
const body = d.bodyText || stripHtml(d.content) || '(empty document)';
|
|
107
|
+
return text(`# ${d.title || 'Untitled'} (id ${d.id})\n\n${body}`);
|
|
108
|
+
}
|
|
109
|
+
if (name === 'workover_list_projects') {
|
|
110
|
+
const projects = await api('/project');
|
|
111
|
+
const list = Array.isArray(projects) ? projects : projects?.projects || [];
|
|
112
|
+
if (!list.length) return text('No projects.');
|
|
113
|
+
return text(list.map((p) => `#${p.id} — ${p.name || p.title || 'Project'}`).join('\n'));
|
|
114
|
+
}
|
|
115
|
+
if (name === 'workover_create_doc') {
|
|
116
|
+
const title = String(args.title || '').trim();
|
|
117
|
+
const md = String(args.markdown || '').trim();
|
|
118
|
+
if (!title || !md) return err('Provide both a `title` and the body as `markdown`.');
|
|
119
|
+
const projectId = rowId(args.projectId);
|
|
120
|
+
if (projectId === null) return badIdErr('project', args.projectId);
|
|
121
|
+
const doc = await apiPost(`/project/${projectId}/documents`, { title });
|
|
122
|
+
if (!doc?.id) return err('Could not create the document.');
|
|
123
|
+
const proposal = await apiPost(`/project/doc/${doc.id}/propose`, {
|
|
124
|
+
markdown: md,
|
|
125
|
+
source: 'Claude Code',
|
|
126
|
+
});
|
|
127
|
+
return text(
|
|
128
|
+
`Created "${title}" (document #${doc.id}) and added the content as suggestions to review.${proposal?.documentUrl ? `\n${proposal.documentUrl}` : ''}\n\nOpen it in Workover to accept or reject the changes — nothing is published.`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
if (name === 'workover_upload_image') {
|
|
132
|
+
const p = String(args.path || '');
|
|
133
|
+
if (!p) return err('Provide the local `path` to the image file.');
|
|
134
|
+
const docId = rowId(args.docId);
|
|
135
|
+
if (docId === null) return badIdErr('document', args.docId);
|
|
136
|
+
let buf;
|
|
137
|
+
try {
|
|
138
|
+
buf = readFile(p);
|
|
139
|
+
} catch {
|
|
140
|
+
return err(`Couldn't read an image at ${p}.`);
|
|
141
|
+
}
|
|
142
|
+
const ext = (p.split('.').pop() || 'png').toLowerCase();
|
|
143
|
+
const mime = mimeForExt(ext);
|
|
144
|
+
const form = new FormData();
|
|
145
|
+
form.append('file', new Blob([buf], { type: mime }), p.split('/').pop() || `image.${ext}`);
|
|
146
|
+
if (args.alt) form.append('alt', String(args.alt));
|
|
147
|
+
const res = await fetchImpl(`${auth.api}/project/doc/${docId}/media`, {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
headers: { authorization: `Bearer ${auth.token}` },
|
|
150
|
+
body: form,
|
|
151
|
+
});
|
|
152
|
+
if (!res.ok) return err(`Upload failed (${res.status}).`);
|
|
153
|
+
const { url } = await res.json();
|
|
154
|
+
return text(
|
|
155
|
+
`Uploaded. Reference it in the document markdown:\n\n`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
if (name === 'workover_propose_edit') {
|
|
159
|
+
const md = String(args.markdown || '').trim();
|
|
160
|
+
if (!md) return err('Provide the full revised document as `markdown`.');
|
|
161
|
+
const docId = rowId(args.docId);
|
|
162
|
+
if (docId === null) return badIdErr('document', args.docId);
|
|
163
|
+
const res = await apiPost(`/project/doc/${docId}/propose`, {
|
|
164
|
+
markdown: md,
|
|
165
|
+
note: args.note ? String(args.note) : undefined,
|
|
166
|
+
source: 'Claude Code',
|
|
167
|
+
});
|
|
168
|
+
return text(
|
|
169
|
+
`Proposed your changes to document #${docId}. They'll appear as suggestions to accept or reject when the document is open in Workover${res?.documentUrl ? `:\n${res.documentUrl}` : '.'}\n\nNothing is published until the person reviews and accepts.`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return err(`Unknown tool: ${name}`);
|
|
173
|
+
} catch (e) {
|
|
174
|
+
return err(e?.message || 'Something went wrong.');
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workover/mcp",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Workover MCP server — let Claude Code and other MCP clients read and cowork on your Workover documents.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"author": "ScaleMath",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
23
|
"index.mjs",
|
|
24
|
+
"lib.mjs",
|
|
24
25
|
"README.md"
|
|
25
26
|
],
|
|
26
27
|
"engines": {
|
|
@@ -31,5 +32,8 @@
|
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@modelcontextprotocol/sdk": "^1.12.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "node --test"
|
|
34
38
|
}
|
|
35
39
|
}
|