@initiativesolutions/crm-mcp-server 0.0.0-stage → 0.5.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/README.md +192 -2
- package/dist/config.d.ts +36 -0
- package/dist/config.js +125 -0
- package/dist/constants.d.ts +51 -0
- package/dist/constants.js +68 -0
- package/dist/errors.d.ts +60 -0
- package/dist/errors.js +399 -0
- package/dist/field-sets.d.ts +37 -0
- package/dist/field-sets.js +97 -0
- package/dist/fold.d.ts +2 -0
- package/dist/fold.js +5 -0
- package/dist/guides.d.ts +37 -0
- package/dist/guides.js +80 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +28 -0
- package/dist/render.d.ts +53 -0
- package/dist/render.js +237 -0
- package/dist/server.d.ts +25 -0
- package/dist/server.js +89 -0
- package/dist/services/api-client.d.ts +42 -0
- package/dist/services/api-client.js +40 -0
- package/dist/services/path.d.ts +9 -0
- package/dist/services/path.js +33 -0
- package/dist/services/query.d.ts +7 -0
- package/dist/services/query.js +56 -0
- package/dist/services/transport.d.ts +48 -0
- package/dist/services/transport.js +264 -0
- package/dist/services/upload-source.d.ts +26 -0
- package/dist/services/upload-source.js +206 -0
- package/dist/services/write-client.d.ts +34 -0
- package/dist/services/write-client.js +57 -0
- package/dist/token-source.d.ts +21 -0
- package/dist/token-source.js +11 -0
- package/dist/tools/deletes.d.ts +3 -0
- package/dist/tools/deletes.js +54 -0
- package/dist/tools/discovery.d.ts +4 -0
- package/dist/tools/discovery.js +165 -0
- package/dist/tools/help.d.ts +3 -0
- package/dist/tools/help.js +96 -0
- package/dist/tools/lines.d.ts +33 -0
- package/dist/tools/lines.js +285 -0
- package/dist/tools/records.d.ts +14 -0
- package/dist/tools/records.js +141 -0
- package/dist/tools/relations.d.ts +4 -0
- package/dist/tools/relations.js +97 -0
- package/dist/tools/schemas.d.ts +76 -0
- package/dist/tools/schemas.js +59 -0
- package/dist/tools/sync.d.ts +4 -0
- package/dist/tools/sync.js +107 -0
- package/dist/tools/writes.d.ts +4 -0
- package/dist/tools/writes.js +353 -0
- package/dist/validation.d.ts +15 -0
- package/dist/validation.js +80 -0
- package/dist/write-wiring.d.ts +4 -0
- package/dist/write-wiring.js +5 -0
- package/docs/partners/en/install.md +206 -0
- package/docs/partners/fr/install.md +206 -0
- package/guides/en/best-practices.md +134 -0
- package/guides/en/converting-leads.md +189 -0
- package/guides/en/fields-and-formats.md +365 -0
- package/guides/en/inventory-documents.md +252 -0
- package/guides/en/linking-records.md +266 -0
- package/guides/en/price-books.md +233 -0
- package/guides/en/querying-records.md +197 -0
- package/guides/en/syncing-data.md +276 -0
- package/guides/en/uploading-documents.md +194 -0
- package/guides/en/writing-records.md +239 -0
- package/guides/fr/best-practices.md +134 -0
- package/guides/fr/converting-leads.md +189 -0
- package/guides/fr/fields-and-formats.md +365 -0
- package/guides/fr/inventory-documents.md +252 -0
- package/guides/fr/linking-records.md +266 -0
- package/guides/fr/price-books.md +233 -0
- package/guides/fr/querying-records.md +197 -0
- package/guides/fr/syncing-data.md +277 -0
- package/guides/fr/uploading-documents.md +194 -0
- package/guides/fr/writing-records.md +239 -0
- package/guides/manifest.json +174 -0
- package/package.json +54 -4
package/README.md
CHANGED
|
@@ -1,3 +1,193 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Initiative CRM MCP server
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This server connects an AI assistant to your Initiative CRM. You ask questions about your
|
|
4
|
+
accounts, contacts, opportunities, quotes and invoices in plain language. The assistant reads
|
|
5
|
+
them live through the Initiative Public API. It acts as you, with exactly your permissions.
|
|
6
|
+
|
|
7
|
+
**Preview.** This is an early release for design partners, integrators and Initiative staff. It
|
|
8
|
+
runs on your own computer. It works with assistants that you install locally: Claude Desktop,
|
|
9
|
+
Claude Code and Cursor. Web assistants such as ChatGPT and claude.ai cannot connect to it yet.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
The install guide is in this package, in English and in French:
|
|
14
|
+
|
|
15
|
+
- `docs/partners/en/install.md`
|
|
16
|
+
- `docs/partners/fr/install.md`
|
|
17
|
+
|
|
18
|
+
Follow the guide for your assistant. The assistant starts the server with this command:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npx -y @initiativesolutions/crm-mcp-server@0.5.1
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The command names a fixed version. Change the number only when Initiative announces a new version.
|
|
25
|
+
|
|
26
|
+
The server needs one setting: `INITIATIVE_API_TOKEN`, your Initiative API token. To create a
|
|
27
|
+
token, go to **My Preferences, then API Tokens** in Initiative. The token contains the name of
|
|
28
|
+
your instance. The address of the Initiative API has a default value, so you do not need to give
|
|
29
|
+
it.
|
|
30
|
+
|
|
31
|
+
## Settings
|
|
32
|
+
|
|
33
|
+
| Setting | Default | What it does |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| `INITIATIVE_API_TOKEN` | *required* | Your API token. The server checks its shape when it starts, so a mistyped token fails at once with a clear message. |
|
|
36
|
+
| `INITIATIVE_BASE_URL` | `https://app.initiative-crm.com/api/v1` | The address of the Initiative API. Change it only for a test environment. |
|
|
37
|
+
| `INITIATIVE_ALLOW_WRITES` | `false` | Allows the assistant to create, update and link records, add notes, upload documents, convert leads and replace the lines of a quote, invoice or order. |
|
|
38
|
+
| `INITIATIVE_ALLOW_DELETES` | `false` | Allows the assistant to delete records and remove links. This switch is separate on purpose, because a delete is permanent. |
|
|
39
|
+
| `INITIATIVE_UPLOAD_DIR` | *not set (off)* | The absolute path of the one folder from which `initiative_upload_document` may read files. |
|
|
40
|
+
| `INITIATIVE_MAX_RESPONSE_CHARS` | `25000` | The maximum size of one answer, in characters. |
|
|
41
|
+
|
|
42
|
+
**The server starts read-only.** When `INITIATIVE_ALLOW_WRITES` is off, the assistant does not
|
|
43
|
+
see any action that changes data. Nobody can make it use such an action. Creating a contact and
|
|
44
|
+
deleting an invoice are not the same risk, so deleting needs a second switch. See
|
|
45
|
+
[Changing data](#changing-data).
|
|
46
|
+
|
|
47
|
+
**Uploading from a file path is off by default.** Other people write part of the text in the CRM.
|
|
48
|
+
A hidden instruction in that text could make an assistant upload a private file, for example an
|
|
49
|
+
SSH key. For this reason, `initiative_upload_document` reads a `file_path` only inside the folder
|
|
50
|
+
of `INITIATIVE_UPLOAD_DIR`. It refuses hidden files and links that lead out of the folder. A path
|
|
51
|
+
outside the folder gets the same refusal, whether the file exists or not. A hard link inside the
|
|
52
|
+
folder counts as a file of the folder. Only a person who can write to the folder can create one.
|
|
53
|
+
On Linux, the server also checks where the opened file really is. On systems without `/proc`
|
|
54
|
+
(macOS and Windows), a local program that can change folders inside the upload folder at the
|
|
55
|
+
right moment could win a race against an upload. When `INITIATIVE_UPLOAD_DIR` is not set, the
|
|
56
|
+
assistant can still send a file as `content_base64`, up to 10 MB.
|
|
57
|
+
|
|
58
|
+
Use a folder that you keep for uploads only. Do not use your home folder or your Documents
|
|
59
|
+
folder, because every file in the upload folder can be sent to the CRM. The folder limits this
|
|
60
|
+
server only. If your assistant also has file or shell tools, it can read files through those
|
|
61
|
+
tools, and this setting does not stop that.
|
|
62
|
+
|
|
63
|
+
## Keeping your token safe
|
|
64
|
+
|
|
65
|
+
Your API token acts as you, with exactly your CRM permissions. An Initiative token cannot be made
|
|
66
|
+
read-only. This is why the server refuses to change anything by default. Treat the token like a
|
|
67
|
+
password:
|
|
68
|
+
|
|
69
|
+
- It is stored in plain text in the configuration file of your assistant. Anyone who can read
|
|
70
|
+
that file can act as you in the CRM.
|
|
71
|
+
- Never paste it into a chat message, an issue or a screenshot.
|
|
72
|
+
- If another person may have seen it, delete it in Initiative under **My Preferences, then API
|
|
73
|
+
Tokens**, and create a new one. The deletion takes effect at once.
|
|
74
|
+
|
|
75
|
+
The server stores nothing between conversations. It sends your data only to your own Initiative
|
|
76
|
+
instance.
|
|
77
|
+
|
|
78
|
+
## If it does not start
|
|
79
|
+
|
|
80
|
+
The server checks its configuration before it accepts a question. A configuration problem stops
|
|
81
|
+
it with the exit status **78** and a message on **stderr**. Some assistants hide this message. If
|
|
82
|
+
the server appears in your assistant but never answers, look for the message here:
|
|
83
|
+
|
|
84
|
+
- **Claude Desktop:** Settings, then Developer, then open the MCP log for `initiative`.
|
|
85
|
+
- **Claude Code:** the message appears in the terminal when it starts.
|
|
86
|
+
- **Cursor:** the Output panel, channel MCP.
|
|
87
|
+
|
|
88
|
+
The two most common causes are a missing `INITIATIVE_API_TOKEN` and a token with the wrong
|
|
89
|
+
shape. A token starts with `itv_`, then a code, an underscore, then 64 hexadecimal characters.
|
|
90
|
+
Paste the whole value, with no spaces or line breaks.
|
|
91
|
+
|
|
92
|
+
## What it can do
|
|
93
|
+
|
|
94
|
+
With both switches off, the server offers eleven actions. All of them only read. Seven more
|
|
95
|
+
actions change data and two delete data (see [Changing data](#changing-data)).
|
|
96
|
+
|
|
97
|
+
- **Who am I:** the CRM instance of this connection, and the user it acts as.
|
|
98
|
+
- **List modules:** what the API reaches, and what each module allows.
|
|
99
|
+
- **Describe a module:** every field, its type, whether it is mandatory, the values a dropdown
|
|
100
|
+
accepts and the target of a link. Your own custom fields are included.
|
|
101
|
+
- **List records:** the records of one module, the most recent change first, with simple filters.
|
|
102
|
+
- **Search records:** the same, with AND and OR groups, for example a date range on one field.
|
|
103
|
+
- **Get one record:** every filled field of one record, including the lines of a quote or invoice.
|
|
104
|
+
- **List related records:** the records linked to one record, for example the invoices of an
|
|
105
|
+
account.
|
|
106
|
+
- **List related modules:** the modules to which a module can be linked.
|
|
107
|
+
- **List deleted records:** the records deleted since a date. Ordinary lists never show them.
|
|
108
|
+
- **Look up countries or currencies:** the ids that country and currency fields need.
|
|
109
|
+
- **Help:** the API guides, in English or French, on request. They are part of the package and
|
|
110
|
+
are copies of the Initiative documentation.
|
|
111
|
+
|
|
112
|
+
When a request fails, the answer always says what went wrong and what to do next. When the API
|
|
113
|
+
answered, the answer also gives the request id. Support can use it to find the exact request.
|
|
114
|
+
When no request was made, for example because the server cannot accept the arguments, the answer
|
|
115
|
+
says so. The server waits a short time for the rate limit without a message. It reports a longer
|
|
116
|
+
wait.
|
|
117
|
+
|
|
118
|
+
A list shows a short set of fields for each module, so that a page of ten records stays small.
|
|
119
|
+
Ask the assistant for more columns, or for one record in full, and it will read them.
|
|
120
|
+
|
|
121
|
+
## Changing data
|
|
122
|
+
|
|
123
|
+
Two switches decide what the assistant can do. When a switch is off, its actions are not in the
|
|
124
|
+
tool list. The assistant does not see them, so no request can make it use them. To turn a switch
|
|
125
|
+
on, set it to `true` in the `env` block of your assistant configuration. Then restart the
|
|
126
|
+
assistant.
|
|
127
|
+
|
|
128
|
+
| Switch | Actions it adds |
|
|
129
|
+
|---|---|
|
|
130
|
+
| `INITIATIVE_ALLOW_WRITES=true` | `initiative_create_record`, `initiative_update_record`, `initiative_link_records`, `initiative_add_comment`, `initiative_upload_document`, `initiative_convert_lead`, `initiative_set_line_items` |
|
|
131
|
+
| `INITIATIVE_ALLOW_DELETES=true` | `initiative_delete_record`, `initiative_unlink_records` |
|
|
132
|
+
|
|
133
|
+
With both switches on, the server offers twenty actions. Each switch works alone. The token
|
|
134
|
+
still carries all your CRM permissions, so the CRM can refuse an action that your user may not do.
|
|
135
|
+
|
|
136
|
+
**Deleting is permanent.** The API has no undo, and the server has none either. A deleted record
|
|
137
|
+
leaves the lists and appears only in `initiative_list_deleted_records`. Converting a lead and
|
|
138
|
+
replacing the lines of a document have no undo either. The assistant is told to ask you before
|
|
139
|
+
each of these actions.
|
|
140
|
+
|
|
141
|
+
**Lines of a quote, invoice or order.** When lines are sent to the CRM, they replace the whole
|
|
142
|
+
list. Nothing is merged. For this reason, lines change only through `initiative_set_line_items`.
|
|
143
|
+
Call it without lines to read the current lines. It gives a list that is ready to send and a
|
|
144
|
+
`lines_version`. Change that list, then send the complete list with the `lines_version`. If
|
|
145
|
+
another person changed the lines in the meantime, the server refuses and tells the assistant to
|
|
146
|
+
read again. A replace keeps only the fields of the list. Every line gets a new id. The CRM
|
|
147
|
+
calculates the tax again from the customer, the item and the date. Extra stored columns are
|
|
148
|
+
lost, for example several discounts on one line, or the link of a converted line to its source.
|
|
149
|
+
One gap remains. The API has no conditional write, so the server cannot detect a change that
|
|
150
|
+
another person makes between its read and its write. That window is one round trip. To create a
|
|
151
|
+
new quote, invoice or order, use `initiative_create_record` with its `line_items`, because the
|
|
152
|
+
CRM refuses a document without a line.
|
|
153
|
+
|
|
154
|
+
**An unknown result is never repeated.** Sometimes a write times out, or the connection breaks
|
|
155
|
+
after the request left. The server then cannot know if the CRM applied the change. It does not
|
|
156
|
+
send the request again. The answer tells the assistant to read the record first. For lines, it
|
|
157
|
+
tells the assistant to read them with `initiative_set_line_items` before it tries again.
|
|
158
|
+
|
|
159
|
+
**Uploading a document.** `initiative_upload_document` accepts content in base64 with a file
|
|
160
|
+
name, up to 10 MB. This always works. It reads a file from disk only when `INITIATIVE_UPLOAD_DIR`
|
|
161
|
+
names a folder, and then only inside that folder.
|
|
162
|
+
|
|
163
|
+
## Development
|
|
164
|
+
|
|
165
|
+
Node 20 or later is required. The server is developed on Node 24.
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
npm install
|
|
169
|
+
npm test # vitest
|
|
170
|
+
npm run check # typecheck
|
|
171
|
+
npm run build # compile to dist/
|
|
172
|
+
npm run smoke # live check of the read actions against the instance in .env.local
|
|
173
|
+
npm run smoke:writes # live check of the write and delete actions on a test instance; deletes what it creates
|
|
174
|
+
npm run measure # size of the tool list, per tool and in total
|
|
175
|
+
npm run measure -- --writes --deletes # the same with both switches on
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Size of the tool list (the serialised tool definitions that the assistant carries), measured
|
|
179
|
+
for v0.4.0:
|
|
180
|
+
|
|
181
|
+
| Posture | Tools | Bytes |
|
|
182
|
+
|---|---|---|
|
|
183
|
+
| Read-only (default) | 11 | 8,862 |
|
|
184
|
+
| `INITIATIVE_ALLOW_WRITES` | 18 | 16,940 |
|
|
185
|
+
| Both switches on | 20 | 18,204 |
|
|
186
|
+
|
|
187
|
+
There is no fixed ceiling. `npm run smoke:writes` refuses any token that is not for a test
|
|
188
|
+
instance, and any production host.
|
|
189
|
+
|
|
190
|
+
`.env.local` is never committed. It holds `INITIATIVE_API_TOKEN` and, for a CRM that is not
|
|
191
|
+
production, an `INITIATIVE_BASE_URL`. If that CRM uses a certificate that your computer does not
|
|
192
|
+
trust, set `NODE_EXTRA_CA_CERTS` to the certificate authority that signed it before you start
|
|
193
|
+
node.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A configuration problem the person who installed the connector has to fix. Thrown at startup,
|
|
3
|
+
* before any transport is connected, so the message reaches their terminal rather than an
|
|
4
|
+
* assistant's tool output.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ConfigError extends Error {
|
|
7
|
+
constructor(message: string);
|
|
8
|
+
}
|
|
9
|
+
export interface Config {
|
|
10
|
+
/** Base URL with no trailing slash, e.g. https://app.initiative-crm.com/api/v1 */
|
|
11
|
+
readonly baseUrl: string;
|
|
12
|
+
readonly token: string;
|
|
13
|
+
/** Decoded from the token's middle segment; the API confirms it in GET /me meta. */
|
|
14
|
+
readonly instanceName: string;
|
|
15
|
+
readonly allowWrites: boolean;
|
|
16
|
+
readonly allowDeletes: boolean;
|
|
17
|
+
readonly maxResponseChars: number;
|
|
18
|
+
/**
|
|
19
|
+
* Real path of the one folder initiative_upload_document may read files from, or null when
|
|
20
|
+
* uploading from a file path is switched off.
|
|
21
|
+
*/
|
|
22
|
+
readonly uploadDir: string | null;
|
|
23
|
+
/**
|
|
24
|
+
* The upload folder as configured, normalised but not resolved, when it differs from
|
|
25
|
+
* uploadDir (on macOS /tmp is a link to /private/tmp). A path given under this name passes the
|
|
26
|
+
* first, lexical check; the real-path check that follows is always against uploadDir.
|
|
27
|
+
*/
|
|
28
|
+
readonly uploadDirAsGiven?: string | null;
|
|
29
|
+
}
|
|
30
|
+
/** Reads the CRM instance out of a token without contacting the API. */
|
|
31
|
+
export declare function instanceNameFromToken(token: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Builds the configuration from the environment, failing loudly on anything wrong. The safe
|
|
34
|
+
* configuration — read-only, production, 25k cap — is the one that requires no decisions.
|
|
35
|
+
*/
|
|
36
|
+
export declare function loadConfig(env?: NodeJS.ProcessEnv): Config;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, resolve } from 'node:path';
|
|
3
|
+
import { DEFAULT_BASE_URL, DEFAULT_MAX_RESPONSE_CHARS } from './constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* A configuration problem the person who installed the connector has to fix. Thrown at startup,
|
|
6
|
+
* before any transport is connected, so the message reaches their terminal rather than an
|
|
7
|
+
* assistant's tool output.
|
|
8
|
+
*/
|
|
9
|
+
export class ConfigError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'ConfigError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* itv_ + unpadded base64 of the instance name + _ + 64 lowercase hex characters.
|
|
17
|
+
* Mirrors ApiTokenService::createToken() and PublicApiAuthenticator::authenticate().
|
|
18
|
+
*/
|
|
19
|
+
const TOKEN_PATTERN = /^itv_([A-Za-z0-9+/]+)_([0-9a-f]{64})$/;
|
|
20
|
+
const BAD_TOKEN_MESSAGE = 'INITIATIVE_API_TOKEN is not a valid Initiative API token. It must start with itv_, then a code, ' +
|
|
21
|
+
'an underscore, then 64 hexadecimal characters. Create one in Initiative under ' +
|
|
22
|
+
'My Preferences -> API Tokens and paste the whole value, with no spaces.';
|
|
23
|
+
/** Control characters and DEL — a decoded segment holding any of these is not an instance name. */
|
|
24
|
+
const NOT_AN_INSTANCE_NAME = /[\u0000-\u001F\u007F]/;
|
|
25
|
+
/** Reads the CRM instance out of a token without contacting the API. */
|
|
26
|
+
export function instanceNameFromToken(token) {
|
|
27
|
+
const match = TOKEN_PATTERN.exec(token);
|
|
28
|
+
if (match === null) {
|
|
29
|
+
throw new ConfigError(BAD_TOKEN_MESSAGE);
|
|
30
|
+
}
|
|
31
|
+
const decoded = Buffer.from(match[1], 'base64').toString('utf8');
|
|
32
|
+
// Base64 decoding is lenient, so check the result is a plausible instance name rather than
|
|
33
|
+
// trusting that valid-looking base64 decoded to anything meaningful.
|
|
34
|
+
if (decoded === '' || NOT_AN_INSTANCE_NAME.test(decoded)) {
|
|
35
|
+
throw new ConfigError(BAD_TOKEN_MESSAGE);
|
|
36
|
+
}
|
|
37
|
+
return decoded;
|
|
38
|
+
}
|
|
39
|
+
const TRUE_VALUES = new Set(['true', '1', 'yes', 'on']);
|
|
40
|
+
const FALSE_VALUES = new Set(['false', '0', 'no', 'off', '']);
|
|
41
|
+
function readSwitch(env, name) {
|
|
42
|
+
const raw = (env[name] ?? '').trim().toLowerCase();
|
|
43
|
+
if (TRUE_VALUES.has(raw)) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (FALSE_VALUES.has(raw)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
throw new ConfigError(`${name} must be one of true/false, 1/0, yes/no, on/off — got "${env[name]}". ` +
|
|
50
|
+
'Leave it unset to keep the safe default.');
|
|
51
|
+
}
|
|
52
|
+
function readBaseUrl(env) {
|
|
53
|
+
const raw = (env.INITIATIVE_BASE_URL ?? DEFAULT_BASE_URL).trim().replace(/\/+$/, '');
|
|
54
|
+
let parsed;
|
|
55
|
+
try {
|
|
56
|
+
parsed = new URL(raw);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new ConfigError(`INITIATIVE_BASE_URL is not a valid URL: "${raw}".`);
|
|
60
|
+
}
|
|
61
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
62
|
+
throw new ConfigError(`INITIATIVE_BASE_URL must be an http or https URL, got "${raw}".`);
|
|
63
|
+
}
|
|
64
|
+
return raw;
|
|
65
|
+
}
|
|
66
|
+
function readMaxResponseChars(env) {
|
|
67
|
+
const raw = (env.INITIATIVE_MAX_RESPONSE_CHARS ?? '').trim();
|
|
68
|
+
if (raw === '') {
|
|
69
|
+
return DEFAULT_MAX_RESPONSE_CHARS;
|
|
70
|
+
}
|
|
71
|
+
if (!/^\d+$/.test(raw)) {
|
|
72
|
+
throw new ConfigError(`INITIATIVE_MAX_RESPONSE_CHARS must be a whole number, got "${raw}".`);
|
|
73
|
+
}
|
|
74
|
+
const value = Number.parseInt(raw, 10);
|
|
75
|
+
if (value < 1_000) {
|
|
76
|
+
throw new ConfigError(`INITIATIVE_MAX_RESPONSE_CHARS must be at least 1000, got ${value}. Below that no useful ` +
|
|
77
|
+
'answer fits and every reply would be a truncation notice.');
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
function readUploadDir(env) {
|
|
82
|
+
const raw = (env.INITIATIVE_UPLOAD_DIR ?? '').trim();
|
|
83
|
+
if (raw === '') {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (!isAbsolute(raw)) {
|
|
87
|
+
throw new ConfigError(`INITIATIVE_UPLOAD_DIR must be an absolute path, got "${raw}".`);
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
if (!statSync(raw).isDirectory()) {
|
|
91
|
+
throw new ConfigError(`INITIATIVE_UPLOAD_DIR must be a directory, but "${raw}" is not one.`);
|
|
92
|
+
}
|
|
93
|
+
const real = realpathSync(raw);
|
|
94
|
+
const given = resolve(raw);
|
|
95
|
+
return { real, given: given === real ? null : given };
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error instanceof ConfigError)
|
|
99
|
+
throw error;
|
|
100
|
+
throw new ConfigError(`INITIATIVE_UPLOAD_DIR "${raw}" does not exist or cannot be read.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Builds the configuration from the environment, failing loudly on anything wrong. The safe
|
|
105
|
+
* configuration — read-only, production, 25k cap — is the one that requires no decisions.
|
|
106
|
+
*/
|
|
107
|
+
export function loadConfig(env = process.env) {
|
|
108
|
+
const token = (env.INITIATIVE_API_TOKEN ?? '').trim();
|
|
109
|
+
if (token === '') {
|
|
110
|
+
throw new ConfigError('INITIATIVE_API_TOKEN is not set. Create an API token in Initiative under ' +
|
|
111
|
+
"My Preferences -> API Tokens and add it to your assistant's MCP server configuration.");
|
|
112
|
+
}
|
|
113
|
+
const uploadDir = readUploadDir(env);
|
|
114
|
+
return {
|
|
115
|
+
baseUrl: readBaseUrl(env),
|
|
116
|
+
token,
|
|
117
|
+
instanceName: instanceNameFromToken(token),
|
|
118
|
+
allowWrites: readSwitch(env, 'INITIATIVE_ALLOW_WRITES'),
|
|
119
|
+
allowDeletes: readSwitch(env, 'INITIATIVE_ALLOW_DELETES'),
|
|
120
|
+
maxResponseChars: readMaxResponseChars(env),
|
|
121
|
+
uploadDir: uploadDir?.real ?? null,
|
|
122
|
+
uploadDirAsGiven: uploadDir?.given ?? null,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Identity this server reports to the assistant during initialisation. */
|
|
2
|
+
export declare const SERVER_NAME = "initiative-mcp-server";
|
|
3
|
+
export declare const SERVER_VERSION = "0.5.1";
|
|
4
|
+
/**
|
|
5
|
+
* Production Initiative CRM Public API v1, overridable with INITIATIVE_BASE_URL.
|
|
6
|
+
*
|
|
7
|
+
* This is production because a customer must be able to paste a token and stop — criterion #1.
|
|
8
|
+
* It is deliberately NOT what development uses: `.env.local` points every local check at the
|
|
9
|
+
* development instance instead, and no test fixture in this repository names a reachable CRM.
|
|
10
|
+
* Staging is https://test.initiative.solutions/api/v1.
|
|
11
|
+
*/
|
|
12
|
+
export declare const DEFAULT_BASE_URL = "https://app.initiative-crm.com/api/v1";
|
|
13
|
+
/**
|
|
14
|
+
* The sixteen module slugs Public API v1 exposes, in the order GET /modules returns them.
|
|
15
|
+
*
|
|
16
|
+
* This list is fixed for every instance — it mirrors ModuleMap::SLUG_TO_MODULE in the CRM,
|
|
17
|
+
* not a per-customer configuration. Holding it here rather than fetching it lets the tool
|
|
18
|
+
* schemas declare `module` as an enum, so the tool list itself documents what exists and a
|
|
19
|
+
* wrong slug is rejected before it costs a round trip.
|
|
20
|
+
*/
|
|
21
|
+
export declare const MODULE_SLUGS: readonly ["leads", "contacts", "accounts", "potentials", "quotes", "invoices", "purchase-orders", "sales-orders", "products", "services", "pricebooks", "documents", "helpdesk", "events", "tasks", "comments"];
|
|
22
|
+
export type ModuleSlug = (typeof MODULE_SLUGS)[number];
|
|
23
|
+
/**
|
|
24
|
+
* Records per page when the assistant has not asked for a number. The API's own default is 20,
|
|
25
|
+
* which is twice what a conversation can afford.
|
|
26
|
+
*/
|
|
27
|
+
export declare const DEFAULT_PER_PAGE = 10;
|
|
28
|
+
/** Cap on the characters any single tool answer may return. Overridable per install. */
|
|
29
|
+
export declare const DEFAULT_MAX_RESPONSE_CHARS = 25000;
|
|
30
|
+
/** How long to wait on one API call before giving up and saying so. */
|
|
31
|
+
export declare const REQUEST_TIMEOUT_MS = 30000;
|
|
32
|
+
/** Longest a single table cell may be, in characters. Longer values end with an ellipsis. */
|
|
33
|
+
export declare const CELL_MAX_CHARS = 80;
|
|
34
|
+
/** The API refuses a per_page above this number and silently uses it instead. */
|
|
35
|
+
export declare const MAX_PER_PAGE = 100;
|
|
36
|
+
/** Most field problems a failure lists; the rest are counted, not printed. */
|
|
37
|
+
export declare const DETAILS_SHOWN = 10;
|
|
38
|
+
/**
|
|
39
|
+
* Longest wait the client absorbs silently, in milliseconds: before a request when the bucket is
|
|
40
|
+
* empty, or on a 429 with a short Retry-After. Anything longer is reported, not waited out.
|
|
41
|
+
*/
|
|
42
|
+
export declare const PACING_MAX_WAIT_MS = 2000;
|
|
43
|
+
/** Most elements (keys and array items) one tool call's arguments may hold; bigger input is refused unread. */
|
|
44
|
+
export declare const MAX_TOOL_INPUT_ELEMENTS = 5000;
|
|
45
|
+
/**
|
|
46
|
+
* Most lines initiative_set_line_items sends in one call. Each call replaces the whole list, so a
|
|
47
|
+
* document with more lines cannot be split over several calls; it is edited in the web interface.
|
|
48
|
+
*/
|
|
49
|
+
export declare const MAX_LINES_PER_CALL = 200;
|
|
50
|
+
/** Largest file initiative_upload_document sends, from a path or from inline base64 (decoded size). */
|
|
51
|
+
export declare const MAX_UPLOAD_BYTES: number;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Identity this server reports to the assistant during initialisation. */
|
|
2
|
+
export const SERVER_NAME = 'initiative-mcp-server';
|
|
3
|
+
export const SERVER_VERSION = '0.5.1';
|
|
4
|
+
/**
|
|
5
|
+
* Production Initiative CRM Public API v1, overridable with INITIATIVE_BASE_URL.
|
|
6
|
+
*
|
|
7
|
+
* This is production because a customer must be able to paste a token and stop — criterion #1.
|
|
8
|
+
* It is deliberately NOT what development uses: `.env.local` points every local check at the
|
|
9
|
+
* development instance instead, and no test fixture in this repository names a reachable CRM.
|
|
10
|
+
* Staging is https://test.initiative.solutions/api/v1.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_BASE_URL = 'https://app.initiative-crm.com/api/v1';
|
|
13
|
+
/**
|
|
14
|
+
* The sixteen module slugs Public API v1 exposes, in the order GET /modules returns them.
|
|
15
|
+
*
|
|
16
|
+
* This list is fixed for every instance — it mirrors ModuleMap::SLUG_TO_MODULE in the CRM,
|
|
17
|
+
* not a per-customer configuration. Holding it here rather than fetching it lets the tool
|
|
18
|
+
* schemas declare `module` as an enum, so the tool list itself documents what exists and a
|
|
19
|
+
* wrong slug is rejected before it costs a round trip.
|
|
20
|
+
*/
|
|
21
|
+
export const MODULE_SLUGS = [
|
|
22
|
+
'leads',
|
|
23
|
+
'contacts',
|
|
24
|
+
'accounts',
|
|
25
|
+
'potentials',
|
|
26
|
+
'quotes',
|
|
27
|
+
'invoices',
|
|
28
|
+
'purchase-orders',
|
|
29
|
+
'sales-orders',
|
|
30
|
+
'products',
|
|
31
|
+
'services',
|
|
32
|
+
'pricebooks',
|
|
33
|
+
'documents',
|
|
34
|
+
'helpdesk',
|
|
35
|
+
'events',
|
|
36
|
+
'tasks',
|
|
37
|
+
'comments',
|
|
38
|
+
];
|
|
39
|
+
/**
|
|
40
|
+
* Records per page when the assistant has not asked for a number. The API's own default is 20,
|
|
41
|
+
* which is twice what a conversation can afford.
|
|
42
|
+
*/
|
|
43
|
+
export const DEFAULT_PER_PAGE = 10;
|
|
44
|
+
/** Cap on the characters any single tool answer may return. Overridable per install. */
|
|
45
|
+
export const DEFAULT_MAX_RESPONSE_CHARS = 25_000;
|
|
46
|
+
/** How long to wait on one API call before giving up and saying so. */
|
|
47
|
+
export const REQUEST_TIMEOUT_MS = 30_000;
|
|
48
|
+
/** Longest a single table cell may be, in characters. Longer values end with an ellipsis. */
|
|
49
|
+
export const CELL_MAX_CHARS = 80;
|
|
50
|
+
/** The API refuses a per_page above this number and silently uses it instead. */
|
|
51
|
+
export const MAX_PER_PAGE = 100;
|
|
52
|
+
/** Most field problems a failure lists; the rest are counted, not printed. */
|
|
53
|
+
export const DETAILS_SHOWN = 10;
|
|
54
|
+
/**
|
|
55
|
+
* Longest wait the client absorbs silently, in milliseconds: before a request when the bucket is
|
|
56
|
+
* empty, or on a 429 with a short Retry-After. Anything longer is reported, not waited out.
|
|
57
|
+
*/
|
|
58
|
+
export const PACING_MAX_WAIT_MS = 2_000;
|
|
59
|
+
/** Most elements (keys and array items) one tool call's arguments may hold; bigger input is refused unread. */
|
|
60
|
+
export const MAX_TOOL_INPUT_ELEMENTS = 5000;
|
|
61
|
+
/**
|
|
62
|
+
* Most lines initiative_set_line_items sends in one call. Each call replaces the whole list, so a
|
|
63
|
+
* document with more lines cannot be split over several calls; it is edited in the web interface.
|
|
64
|
+
*/
|
|
65
|
+
export const MAX_LINES_PER_CALL = 200;
|
|
66
|
+
/** Largest file initiative_upload_document sends, from a path or from inline base64 (decoded size). */
|
|
67
|
+
export const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
|
|
68
|
+
//# sourceMappingURL=constants.js.map
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface ErrorContext {
|
|
2
|
+
/** Set when the failing call named a module, so a 404 can list the valid slugs. */
|
|
3
|
+
module?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ErrorDetail {
|
|
6
|
+
field: string;
|
|
7
|
+
code: string;
|
|
8
|
+
message: string;
|
|
9
|
+
}
|
|
10
|
+
export interface InitiativeApiErrorInit {
|
|
11
|
+
status: number | null;
|
|
12
|
+
code: string;
|
|
13
|
+
message: string;
|
|
14
|
+
requestId: string | null;
|
|
15
|
+
retryable: boolean;
|
|
16
|
+
nextStep: string;
|
|
17
|
+
retryAfterSeconds?: number | null;
|
|
18
|
+
details?: ErrorDetail[];
|
|
19
|
+
/** False when no HTTP request left this machine. Defaults to true. */
|
|
20
|
+
requestSent?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A failure the assistant can act on: what happened, whether retrying is worth anything, what to
|
|
24
|
+
* do instead, and the request id support can find in our logs.
|
|
25
|
+
*/
|
|
26
|
+
export declare class InitiativeApiError extends Error {
|
|
27
|
+
readonly status: number | null;
|
|
28
|
+
readonly code: string;
|
|
29
|
+
readonly requestId: string | null;
|
|
30
|
+
readonly retryable: boolean;
|
|
31
|
+
readonly nextStep: string;
|
|
32
|
+
readonly retryAfterSeconds: number | null;
|
|
33
|
+
readonly details: ErrorDetail[];
|
|
34
|
+
readonly requestSent: boolean;
|
|
35
|
+
constructor(init: InitiativeApiErrorInit);
|
|
36
|
+
}
|
|
37
|
+
/** Turns a non-2xx HTTP response into something an assistant can recover from. */
|
|
38
|
+
export declare function mapHttpFailure(status: number, rawBody: string, headers: Headers, context?: ErrorContext): InitiativeApiError;
|
|
39
|
+
/** A request that failed in transit: DNS, TLS, a refused or dropped connection, or the timeout. */
|
|
40
|
+
export declare function networkFailure(cause: unknown, url: string): InitiativeApiError;
|
|
41
|
+
/** The text an assistant sees. Order matters: what happened, what to do, the request id. */
|
|
42
|
+
export declare function formatToolFailure(error: InitiativeApiError): string;
|
|
43
|
+
/**
|
|
44
|
+
* Wraps any failure as an MCP tool result. Every tool handler ends here on its error path,
|
|
45
|
+
* including for exceptions that are not InitiativeApiError — a bug in this connector must still
|
|
46
|
+
* reach the assistant as a readable answer rather than as a protocol-level error.
|
|
47
|
+
*/
|
|
48
|
+
export declare function toToolResult(error: unknown): {
|
|
49
|
+
isError: true;
|
|
50
|
+
content: Array<{
|
|
51
|
+
type: 'text';
|
|
52
|
+
text: string;
|
|
53
|
+
}>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* initiative_set_line_items refuses a replace when the lines changed after the caller read them.
|
|
57
|
+
* The connector read the document to compare (that GET was sent, so requestSent is true and the
|
|
58
|
+
* request id is the read's), but it did not send the write.
|
|
59
|
+
*/
|
|
60
|
+
export declare function linesChanged(module: string, id: string, sent: string, current: string, requestId: string | null): InitiativeApiError;
|