@workover/mcp 0.2.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/lib.mjs +177 -0
- package/package.json +2 -1
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.2.
|
|
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": {
|