@mindstone-engineering/mcp-server-pandadoc 0.1.0
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/dist/auth.d.ts +19 -0
- package/dist/auth.js +26 -0
- package/dist/bridge.d.ts +16 -0
- package/dist/bridge.js +43 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.js +113 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +26 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +13 -0
- package/dist/tools/configure.d.ts +3 -0
- package/dist/tools/configure.js +47 -0
- package/dist/tools/documents.d.ts +3 -0
- package/dist/tools/documents.js +444 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +4 -0
- package/dist/tools/templates.d.ts +3 -0
- package/dist/tools/templates.js +66 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +14 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.js +42 -0
- package/package.json +48 -0
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PandaDoc authentication module.
|
|
3
|
+
*
|
|
4
|
+
* Manages the API key lifecycle — env var on startup, runtime update via
|
|
5
|
+
* configure tool, and bridge integration for host-app credential management.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Returns the current API key.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getApiKey(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Returns true if an API key is configured.
|
|
13
|
+
*/
|
|
14
|
+
export declare function isConfigured(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Update the API key at runtime (e.g. after configure_pandadoc_api_key).
|
|
17
|
+
*/
|
|
18
|
+
export declare function setApiKey(key: string): void;
|
|
19
|
+
//# sourceMappingURL=auth.d.ts.map
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PandaDoc authentication module.
|
|
3
|
+
*
|
|
4
|
+
* Manages the API key lifecycle — env var on startup, runtime update via
|
|
5
|
+
* configure tool, and bridge integration for host-app credential management.
|
|
6
|
+
*/
|
|
7
|
+
let apiKey = process.env.PANDADOC_API_KEY || '';
|
|
8
|
+
/**
|
|
9
|
+
* Returns the current API key.
|
|
10
|
+
*/
|
|
11
|
+
export function getApiKey() {
|
|
12
|
+
return apiKey;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if an API key is configured.
|
|
16
|
+
*/
|
|
17
|
+
export function isConfigured() {
|
|
18
|
+
return apiKey.length > 0;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Update the API key at runtime (e.g. after configure_pandadoc_api_key).
|
|
22
|
+
*/
|
|
23
|
+
export function setApiKey(key) {
|
|
24
|
+
apiKey = key;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path to bridge state file, supporting both current and legacy env vars.
|
|
3
|
+
*/
|
|
4
|
+
export declare const BRIDGE_STATE_PATH: string;
|
|
5
|
+
/**
|
|
6
|
+
* Send a request to the host app bridge.
|
|
7
|
+
*
|
|
8
|
+
* The bridge is an HTTP server running inside the host app (e.g. Rebel)
|
|
9
|
+
* that handles credential management and other cross-process operations.
|
|
10
|
+
*/
|
|
11
|
+
export declare const bridgeRequest: (urlPath: string, body: Record<string, unknown>) => Promise<{
|
|
12
|
+
success: boolean;
|
|
13
|
+
warning?: string;
|
|
14
|
+
error?: string;
|
|
15
|
+
}>;
|
|
16
|
+
//# sourceMappingURL=bridge.d.ts.map
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { REQUEST_TIMEOUT_MS } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Path to bridge state file, supporting both current and legacy env vars.
|
|
5
|
+
*/
|
|
6
|
+
export const BRIDGE_STATE_PATH = process.env.MCP_HOST_BRIDGE_STATE || process.env.MINDSTONE_REBEL_BRIDGE_STATE || '';
|
|
7
|
+
const loadBridgeState = () => {
|
|
8
|
+
if (!BRIDGE_STATE_PATH)
|
|
9
|
+
return null;
|
|
10
|
+
try {
|
|
11
|
+
const raw = fs.readFileSync(BRIDGE_STATE_PATH, 'utf8');
|
|
12
|
+
return JSON.parse(raw);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Send a request to the host app bridge.
|
|
20
|
+
*
|
|
21
|
+
* The bridge is an HTTP server running inside the host app (e.g. Rebel)
|
|
22
|
+
* that handles credential management and other cross-process operations.
|
|
23
|
+
*/
|
|
24
|
+
export const bridgeRequest = async (urlPath, body) => {
|
|
25
|
+
const bridge = loadBridgeState();
|
|
26
|
+
if (!bridge) {
|
|
27
|
+
return { success: false, error: 'Bridge not available' };
|
|
28
|
+
}
|
|
29
|
+
const response = await fetch(`http://127.0.0.1:${bridge.port}${urlPath}`, {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
Authorization: `Bearer ${bridge.token}`,
|
|
35
|
+
},
|
|
36
|
+
body: JSON.stringify(body),
|
|
37
|
+
});
|
|
38
|
+
if (response.status === 401 || response.status === 403) {
|
|
39
|
+
return { success: false, error: `Bridge returned ${response.status}: unauthorized. Check host app authentication.` };
|
|
40
|
+
}
|
|
41
|
+
return response.json();
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=bridge.js.map
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PandaDoc API HTTP client.
|
|
3
|
+
*
|
|
4
|
+
* Centralises API-Key header injection, error handling, and rate-limit
|
|
5
|
+
* messaging for all PandaDoc API calls.
|
|
6
|
+
*
|
|
7
|
+
* IMPORTANT: PandaDoc uses `Authorization: API-Key {key}` — not Bearer.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Make an authenticated request to the PandaDoc API.
|
|
11
|
+
*
|
|
12
|
+
* @param path API path relative to base, e.g. `/documents`
|
|
13
|
+
* @param options Additional fetch options
|
|
14
|
+
* @returns Parsed JSON response
|
|
15
|
+
*/
|
|
16
|
+
export declare function pandadocFetch<T>(path: string, options?: RequestInit): Promise<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Make an authenticated raw request (for binary downloads).
|
|
19
|
+
*
|
|
20
|
+
* @param path API path relative to base
|
|
21
|
+
* @param options Additional fetch options
|
|
22
|
+
* @returns Raw Response object
|
|
23
|
+
*/
|
|
24
|
+
export declare function pandadocFetchRaw(path: string, options?: RequestInit): Promise<Response>;
|
|
25
|
+
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PandaDoc API HTTP client.
|
|
3
|
+
*
|
|
4
|
+
* Centralises API-Key header injection, error handling, and rate-limit
|
|
5
|
+
* messaging for all PandaDoc API calls.
|
|
6
|
+
*
|
|
7
|
+
* IMPORTANT: PandaDoc uses `Authorization: API-Key {key}` — not Bearer.
|
|
8
|
+
*/
|
|
9
|
+
import { getApiKey } from './auth.js';
|
|
10
|
+
import { PandaDocError, PANDADOC_API_BASE, REQUEST_TIMEOUT_MS } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Make an authenticated request to the PandaDoc API.
|
|
13
|
+
*
|
|
14
|
+
* @param path API path relative to base, e.g. `/documents`
|
|
15
|
+
* @param options Additional fetch options
|
|
16
|
+
* @returns Parsed JSON response
|
|
17
|
+
*/
|
|
18
|
+
export async function pandadocFetch(path, options = {}) {
|
|
19
|
+
const key = getApiKey();
|
|
20
|
+
if (!key) {
|
|
21
|
+
throw new PandaDocError('PandaDoc API key not configured', 'AUTH_REQUIRED', 'Use configure_pandadoc_api_key to set your API key first.');
|
|
22
|
+
}
|
|
23
|
+
const url = `${PANDADOC_API_BASE}${path}`;
|
|
24
|
+
let response;
|
|
25
|
+
try {
|
|
26
|
+
response = await fetch(url, {
|
|
27
|
+
...options,
|
|
28
|
+
signal: options.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `API-Key ${key}`,
|
|
31
|
+
'Content-Type': 'application/json',
|
|
32
|
+
...options.headers,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
38
|
+
throw new PandaDocError('Request to PandaDoc API timed out', 'TIMEOUT', 'The request took too long. Try again or check if the PandaDoc API is available.');
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
if (response.status === 401 || response.status === 403) {
|
|
43
|
+
throw new PandaDocError('Authentication failed', 'AUTH_FAILED', 'Your PandaDoc API key is invalid or revoked. Use configure_pandadoc_api_key to set a new key.');
|
|
44
|
+
}
|
|
45
|
+
if (response.status === 429) {
|
|
46
|
+
const retryAfter = response.headers.get('Retry-After');
|
|
47
|
+
const parsed = retryAfter ? parseInt(retryAfter, 10) : NaN;
|
|
48
|
+
const waitSeconds = Number.isFinite(parsed) ? parsed : 60;
|
|
49
|
+
throw new PandaDocError(`Rate limited by PandaDoc API. Please wait ${waitSeconds} seconds before retrying.`, 'RATE_LIMITED', `Wait ${waitSeconds} seconds and try again. PandaDoc has per-minute rate limits.`);
|
|
50
|
+
}
|
|
51
|
+
if (response.status === 404) {
|
|
52
|
+
throw new PandaDocError('Resource not found', 'NOT_FOUND', 'The requested document or template does not exist. Verify the ID using list_documents or list_templates.');
|
|
53
|
+
}
|
|
54
|
+
if (response.status === 409) {
|
|
55
|
+
const errorBody = await response.text().catch(() => '');
|
|
56
|
+
throw new PandaDocError(`Conflict (409): ${errorBody || 'Document is not ready for this operation.'}`, 'CONFLICT', 'Check the document status with get_document_status. The document may not be in the required state.');
|
|
57
|
+
}
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const errorText = await response.text().catch(() => 'Unknown error');
|
|
60
|
+
throw new PandaDocError(`PandaDoc API error (${response.status}): ${errorText}`, 'API_ERROR', 'Check the request parameters and try again.');
|
|
61
|
+
}
|
|
62
|
+
// Some endpoints may return empty body
|
|
63
|
+
const text = await response.text();
|
|
64
|
+
if (!text)
|
|
65
|
+
return {};
|
|
66
|
+
return JSON.parse(text);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Make an authenticated raw request (for binary downloads).
|
|
70
|
+
*
|
|
71
|
+
* @param path API path relative to base
|
|
72
|
+
* @param options Additional fetch options
|
|
73
|
+
* @returns Raw Response object
|
|
74
|
+
*/
|
|
75
|
+
export async function pandadocFetchRaw(path, options = {}) {
|
|
76
|
+
const key = getApiKey();
|
|
77
|
+
if (!key) {
|
|
78
|
+
throw new PandaDocError('PandaDoc API key not configured', 'AUTH_REQUIRED', 'Use configure_pandadoc_api_key to set your API key first.');
|
|
79
|
+
}
|
|
80
|
+
const url = `${PANDADOC_API_BASE}${path}`;
|
|
81
|
+
let response;
|
|
82
|
+
try {
|
|
83
|
+
response = await fetch(url, {
|
|
84
|
+
...options,
|
|
85
|
+
signal: options.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
86
|
+
headers: {
|
|
87
|
+
Authorization: `API-Key ${key}`,
|
|
88
|
+
Accept: 'application/pdf',
|
|
89
|
+
...options.headers,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
95
|
+
throw new PandaDocError('Request to PandaDoc API timed out', 'TIMEOUT', 'The request took too long. Try again or check if the PandaDoc API is available.');
|
|
96
|
+
}
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
if (response.status === 401 || response.status === 403) {
|
|
100
|
+
throw new PandaDocError('Authentication failed', 'AUTH_FAILED', 'Your PandaDoc API key is invalid or revoked. Use configure_pandadoc_api_key to set a new key.');
|
|
101
|
+
}
|
|
102
|
+
if (response.status === 409) {
|
|
103
|
+
throw new PandaDocError('Document is not ready for download. It may still be processing or in draft status.', 'CONFLICT', 'Check the document status with get_document_status. Documents must be sent or completed to download.');
|
|
104
|
+
}
|
|
105
|
+
if (response.status === 404) {
|
|
106
|
+
throw new PandaDocError('Document not found.', 'NOT_FOUND', 'Verify the document ID is correct using list_documents.');
|
|
107
|
+
}
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
throw new PandaDocError(`Download failed (${response.status}): ${response.statusText}`, 'DOWNLOAD_ERROR', 'Check the document status and try again.');
|
|
110
|
+
}
|
|
111
|
+
return response;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=client.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* PandaDoc MCP Server
|
|
4
|
+
*
|
|
5
|
+
* Provides PandaDoc document automation via Model Context Protocol.
|
|
6
|
+
* Upload documents, create from templates, send for e-signature,
|
|
7
|
+
* track status, download.
|
|
8
|
+
*
|
|
9
|
+
* Environment variables:
|
|
10
|
+
* - PANDADOC_API_KEY: User's PandaDoc API key — required for all operations
|
|
11
|
+
* - MCP_HOST_BRIDGE_STATE: Path to host app bridge state file (optional)
|
|
12
|
+
* - MINDSTONE_REBEL_BRIDGE_STATE: Legacy bridge state path (optional)
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* PandaDoc MCP Server
|
|
4
|
+
*
|
|
5
|
+
* Provides PandaDoc document automation via Model Context Protocol.
|
|
6
|
+
* Upload documents, create from templates, send for e-signature,
|
|
7
|
+
* track status, download.
|
|
8
|
+
*
|
|
9
|
+
* Environment variables:
|
|
10
|
+
* - PANDADOC_API_KEY: User's PandaDoc API key — required for all operations
|
|
11
|
+
* - MCP_HOST_BRIDGE_STATE: Path to host app bridge state file (optional)
|
|
12
|
+
* - MINDSTONE_REBEL_BRIDGE_STATE: Legacy bridge state path (optional)
|
|
13
|
+
*/
|
|
14
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
15
|
+
import { createServer } from './server.js';
|
|
16
|
+
async function main() {
|
|
17
|
+
const server = createServer();
|
|
18
|
+
const transport = new StdioServerTransport();
|
|
19
|
+
await server.connect(transport);
|
|
20
|
+
console.error('PandaDoc MCP server running on stdio');
|
|
21
|
+
}
|
|
22
|
+
main().catch((error) => {
|
|
23
|
+
console.error('Fatal error:', error);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import { registerConfigureTools, registerDocumentTools, registerTemplateTools, } from './tools/index.js';
|
|
3
|
+
export function createServer() {
|
|
4
|
+
const server = new McpServer({
|
|
5
|
+
name: 'pandadoc-mcp-server',
|
|
6
|
+
version: '0.1.0',
|
|
7
|
+
});
|
|
8
|
+
registerConfigureTools(server);
|
|
9
|
+
registerDocumentTools(server);
|
|
10
|
+
registerTemplateTools(server);
|
|
11
|
+
return server;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { setApiKey } from '../auth.js';
|
|
3
|
+
import { bridgeRequest, BRIDGE_STATE_PATH } from '../bridge.js';
|
|
4
|
+
import { PandaDocError } from '../types.js';
|
|
5
|
+
import { withErrorHandling } from '../utils.js';
|
|
6
|
+
export function registerConfigureTools(server) {
|
|
7
|
+
server.registerTool('configure_pandadoc_api_key', {
|
|
8
|
+
description: 'Configure the PandaDoc API key. Call this tool when the user provides their PandaDoc API key. ' +
|
|
9
|
+
'Get an API key from the PandaDoc Developer Dashboard: ' +
|
|
10
|
+
'Settings → API → Developer Dashboard → Generate a Sandbox key (for testing) or Production key (for live use). ' +
|
|
11
|
+
'Note: API access requires a PandaDoc Business or Enterprise plan.',
|
|
12
|
+
inputSchema: z.object({
|
|
13
|
+
api_key: z.string().min(1).describe('The PandaDoc API key from Settings > API > Developer Dashboard'),
|
|
14
|
+
}),
|
|
15
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
16
|
+
}, withErrorHandling(async (args) => {
|
|
17
|
+
const trimmedKey = args.api_key.trim();
|
|
18
|
+
// If bridge is available, persist via bridge
|
|
19
|
+
if (BRIDGE_STATE_PATH) {
|
|
20
|
+
try {
|
|
21
|
+
const result = await bridgeRequest('/bundled/pandadoc/configure', { apiKey: trimmedKey });
|
|
22
|
+
if (result.success) {
|
|
23
|
+
setApiKey(trimmedKey);
|
|
24
|
+
const message = result.warning
|
|
25
|
+
? `PandaDoc API key configured successfully. Note: ${result.warning}`
|
|
26
|
+
: 'PandaDoc API key configured successfully! You can now use list_documents, upload_document, and other PandaDoc tools.';
|
|
27
|
+
return JSON.stringify({ ok: true, message });
|
|
28
|
+
}
|
|
29
|
+
// Bridge returned failure — surface as error, do NOT fall through
|
|
30
|
+
throw new PandaDocError(result.error || 'Bridge configuration failed', 'BRIDGE_ERROR', 'The host app bridge rejected the configuration request. Check the host app logs.');
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error instanceof PandaDocError)
|
|
34
|
+
throw error;
|
|
35
|
+
// Bridge request failed (network, timeout, etc.) — surface as error
|
|
36
|
+
throw new PandaDocError(`Bridge request failed: ${error instanceof Error ? error.message : String(error)}`, 'BRIDGE_ERROR', 'Could not reach the host app bridge. Ensure the host app is running.');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// No bridge configured — configure in-memory only
|
|
40
|
+
setApiKey(trimmedKey);
|
|
41
|
+
return JSON.stringify({
|
|
42
|
+
ok: true,
|
|
43
|
+
message: 'PandaDoc API key configured successfully! You can now use list_documents, upload_document, and other PandaDoc tools.',
|
|
44
|
+
});
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=configure.js.map
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { pandadocFetch, pandadocFetchRaw } from '../client.js';
|
|
5
|
+
import { withErrorHandling } from '../utils.js';
|
|
6
|
+
import { isConfigured } from '../auth.js';
|
|
7
|
+
import { MAX_FILE_SIZE } from '../types.js';
|
|
8
|
+
function noApiKeyError() {
|
|
9
|
+
return JSON.stringify({
|
|
10
|
+
ok: false,
|
|
11
|
+
error: 'PandaDoc API key not configured',
|
|
12
|
+
resolution: 'To use PandaDoc, you need to configure an API key first.',
|
|
13
|
+
next_step: {
|
|
14
|
+
action: 'Ask the user for their PandaDoc API key, then call configure_pandadoc_api_key',
|
|
15
|
+
tool_to_call: 'configure_pandadoc_api_key',
|
|
16
|
+
tool_parameters: { api_key: '<user_provided_key>' },
|
|
17
|
+
get_key_from: 'PandaDoc Settings → API → Developer Dashboard. Requires Business or Enterprise plan.',
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function formatDocumentCompact(doc) {
|
|
22
|
+
return {
|
|
23
|
+
id: doc.id,
|
|
24
|
+
name: doc.name,
|
|
25
|
+
status: doc.status,
|
|
26
|
+
date_created: doc.date_created,
|
|
27
|
+
date_modified: doc.date_modified,
|
|
28
|
+
expiration_date: doc.expiration_date,
|
|
29
|
+
version: doc.version,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function formatDocumentDetails(doc) {
|
|
33
|
+
return {
|
|
34
|
+
id: doc.id,
|
|
35
|
+
name: doc.name,
|
|
36
|
+
status: doc.status,
|
|
37
|
+
date_created: doc.date_created,
|
|
38
|
+
date_modified: doc.date_modified,
|
|
39
|
+
date_completed: doc.date_completed,
|
|
40
|
+
date_sent: doc.date_sent,
|
|
41
|
+
expiration_date: doc.expiration_date,
|
|
42
|
+
version: doc.version,
|
|
43
|
+
created_by: doc.created_by,
|
|
44
|
+
template: doc.template,
|
|
45
|
+
recipients: doc.recipients,
|
|
46
|
+
fields: doc.fields,
|
|
47
|
+
tokens: doc.tokens,
|
|
48
|
+
metadata: doc.metadata,
|
|
49
|
+
tags: doc.tags,
|
|
50
|
+
grand_total: doc.grand_total,
|
|
51
|
+
linked_objects: doc.linked_objects,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function paginationHint(count, page, pageSize) {
|
|
55
|
+
if (count < pageSize)
|
|
56
|
+
return `Showing all ${count} results.`;
|
|
57
|
+
return `Showing ${count} results (page ${page}). Use page=${page + 1} to see more.`;
|
|
58
|
+
}
|
|
59
|
+
export function registerDocumentTools(server) {
|
|
60
|
+
// ── list_documents ──────────────────────────────────────────────────
|
|
61
|
+
server.registerTool('list_documents', {
|
|
62
|
+
description: `List and search PandaDoc documents with filtering.
|
|
63
|
+
|
|
64
|
+
Supports filtering by status, template, name/reference, tags, metadata, date ranges, and more.
|
|
65
|
+
Returns compact summaries: id, name, status, dates.
|
|
66
|
+
|
|
67
|
+
Status codes: 0=draft, 1=sent, 2=completed, 3=uploaded, 4=error, 5=viewed,
|
|
68
|
+
6=waiting_approval, 7=approved, 8=rejected, 9=waiting_pay, 10=paid, 11=voided, 12=declined
|
|
69
|
+
|
|
70
|
+
RELATED TOOLS:
|
|
71
|
+
- get_document_details: Get full details for a specific document
|
|
72
|
+
- get_document_status: Quick status check by document ID`,
|
|
73
|
+
inputSchema: z.object({
|
|
74
|
+
q: z.string().optional().describe('Search by document name or reference number'),
|
|
75
|
+
status: z.number().optional().describe('Filter by status code (0=draft, 1=sent, 2=completed, etc.)'),
|
|
76
|
+
template_id: z.string().optional().describe('Filter by parent template ID'),
|
|
77
|
+
folder_uuid: z.string().optional().describe('Filter by folder ID'),
|
|
78
|
+
tag: z.string().optional().describe('Filter by tag'),
|
|
79
|
+
created_from: z.string().optional().describe('Documents created on or after this date (ISO 8601)'),
|
|
80
|
+
created_to: z.string().optional().describe('Documents created before this date (ISO 8601)'),
|
|
81
|
+
order_by: z.string().optional().describe('Sort field. Prefix with - for DESC (e.g., "-date_created"). Default: date_status_changed'),
|
|
82
|
+
count: z.number().min(1).max(100).default(50).describe('Results per page (default 50, max 100)'),
|
|
83
|
+
page: z.number().min(1).default(1).describe('Page number (starts at 1)'),
|
|
84
|
+
}),
|
|
85
|
+
annotations: { readOnlyHint: true },
|
|
86
|
+
}, withErrorHandling(async (args) => {
|
|
87
|
+
if (!isConfigured())
|
|
88
|
+
return noApiKeyError();
|
|
89
|
+
const params = new URLSearchParams();
|
|
90
|
+
params.set('count', String(args.count));
|
|
91
|
+
params.set('page', String(args.page));
|
|
92
|
+
if (args.q)
|
|
93
|
+
params.set('q', args.q);
|
|
94
|
+
if (args.status !== undefined)
|
|
95
|
+
params.set('status', String(args.status));
|
|
96
|
+
if (args.template_id)
|
|
97
|
+
params.set('template_id', args.template_id);
|
|
98
|
+
if (args.folder_uuid)
|
|
99
|
+
params.set('folder_uuid', args.folder_uuid);
|
|
100
|
+
if (args.tag)
|
|
101
|
+
params.set('tag', args.tag);
|
|
102
|
+
if (args.created_from)
|
|
103
|
+
params.set('created_from', args.created_from);
|
|
104
|
+
if (args.created_to)
|
|
105
|
+
params.set('created_to', args.created_to);
|
|
106
|
+
if (args.order_by)
|
|
107
|
+
params.set('order_by', args.order_by);
|
|
108
|
+
const result = await pandadocFetch(`/documents?${params.toString()}`);
|
|
109
|
+
const documents = (result.results || []).map(formatDocumentCompact);
|
|
110
|
+
const hint = paginationHint(documents.length, args.page, args.count);
|
|
111
|
+
return JSON.stringify({ ok: true, documents, count: documents.length, pagination: hint });
|
|
112
|
+
}));
|
|
113
|
+
// ── get_document_status ─────────────────────────────────────────────
|
|
114
|
+
server.registerTool('get_document_status', {
|
|
115
|
+
description: `Check the current status of a PandaDoc document.
|
|
116
|
+
|
|
117
|
+
Returns: id, name, status, dates. Lightweight alternative to get_document_details.
|
|
118
|
+
Use this to poll after upload/creation until status changes to 'document.draft'.
|
|
119
|
+
|
|
120
|
+
Status values: document.uploaded, document.draft, document.sent, document.completed,
|
|
121
|
+
document.viewed, document.waiting_approval, document.approved, document.rejected,
|
|
122
|
+
document.waiting_pay, document.paid, document.voided, document.declined, document.error
|
|
123
|
+
|
|
124
|
+
WORKFLOW — After upload:
|
|
125
|
+
1. Upload returns status 'document.uploaded'
|
|
126
|
+
2. Poll this tool every 2-3 seconds
|
|
127
|
+
3. When status is 'document.draft', the document is ready to send`,
|
|
128
|
+
inputSchema: z.object({
|
|
129
|
+
document_id: z.string().min(1).describe('The document ID'),
|
|
130
|
+
}),
|
|
131
|
+
annotations: { readOnlyHint: true },
|
|
132
|
+
}, withErrorHandling(async (args) => {
|
|
133
|
+
if (!isConfigured())
|
|
134
|
+
return noApiKeyError();
|
|
135
|
+
const result = await pandadocFetch(`/documents/${encodeURIComponent(args.document_id)}`);
|
|
136
|
+
return JSON.stringify({ ok: true, document: formatDocumentCompact(result) });
|
|
137
|
+
}));
|
|
138
|
+
// ── get_document_details ────────────────────────────────────────────
|
|
139
|
+
server.registerTool('get_document_details', {
|
|
140
|
+
description: `Get full details for a PandaDoc document.
|
|
141
|
+
|
|
142
|
+
Returns comprehensive data: recipients, fields, tokens, pricing, metadata, tags, linked objects, and more.
|
|
143
|
+
Use this to inspect a document's content and state before sending or after completion.
|
|
144
|
+
|
|
145
|
+
RELATED TOOLS:
|
|
146
|
+
- list_documents: Find document IDs
|
|
147
|
+
- get_document_status: Quick status check (lighter weight than full details)
|
|
148
|
+
- send_document: Send the document for signing`,
|
|
149
|
+
inputSchema: z.object({
|
|
150
|
+
document_id: z.string().min(1).describe('The document ID'),
|
|
151
|
+
}),
|
|
152
|
+
annotations: { readOnlyHint: true },
|
|
153
|
+
}, withErrorHandling(async (args) => {
|
|
154
|
+
if (!isConfigured())
|
|
155
|
+
return noApiKeyError();
|
|
156
|
+
const result = await pandadocFetch(`/documents/${encodeURIComponent(args.document_id)}/details`);
|
|
157
|
+
return JSON.stringify({ ok: true, document: formatDocumentDetails(result) });
|
|
158
|
+
}));
|
|
159
|
+
// ── create_document_from_template ───────────────────────────────────
|
|
160
|
+
server.registerTool('create_document_from_template', {
|
|
161
|
+
description: `Create a new PandaDoc document from an existing template.
|
|
162
|
+
|
|
163
|
+
Templates can contain fields, tokens (variables), pricing tables, images, and content placeholders.
|
|
164
|
+
Pre-fill field values, set recipients, and customize content when creating.
|
|
165
|
+
|
|
166
|
+
WORKFLOW:
|
|
167
|
+
1. Call list_templates to find the template ID
|
|
168
|
+
2. Create the document with this tool (pre-fill fields/tokens as needed)
|
|
169
|
+
3. Poll get_document_status until status is 'document.draft'
|
|
170
|
+
4. Use send_document to send for signing
|
|
171
|
+
|
|
172
|
+
RELATED TOOLS:
|
|
173
|
+
- list_templates: Find available templates and their IDs
|
|
174
|
+
- get_document_details: See all fields/tokens after creation
|
|
175
|
+
- send_document: Send the created document`,
|
|
176
|
+
inputSchema: z.object({
|
|
177
|
+
template_uuid: z.string().min(1).describe('Template ID (from list_templates or PandaDoc app URL)'),
|
|
178
|
+
name: z.string().optional().describe('Document name'),
|
|
179
|
+
recipients: z.array(z.object({
|
|
180
|
+
email: z.string().describe('Recipient email'),
|
|
181
|
+
first_name: z.string().optional().describe('Recipient first name'),
|
|
182
|
+
last_name: z.string().optional().describe('Recipient last name'),
|
|
183
|
+
role: z.string().optional().describe('Must match a role in the template'),
|
|
184
|
+
signing_order: z.number().optional().describe('Order in signing sequence'),
|
|
185
|
+
})).min(1).describe('Document recipients (at least one required)'),
|
|
186
|
+
tokens: z.array(z.object({
|
|
187
|
+
name: z.string().describe('Token/variable name from template'),
|
|
188
|
+
value: z.string().describe('Value to fill in'),
|
|
189
|
+
})).optional().describe('Template variables to pre-fill'),
|
|
190
|
+
fields: z.record(z.unknown()).optional().describe('Map of field names to values: { "FieldName": { "value": "text" } }'),
|
|
191
|
+
metadata: z.record(z.unknown()).optional().describe('Custom key-value metadata to associate with the document'),
|
|
192
|
+
tags: z.array(z.string()).optional().describe('Tags to apply'),
|
|
193
|
+
folder_uuid: z.string().optional().describe('Folder ID to store the document in'),
|
|
194
|
+
}),
|
|
195
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
196
|
+
}, withErrorHandling(async (args) => {
|
|
197
|
+
if (!isConfigured())
|
|
198
|
+
return noApiKeyError();
|
|
199
|
+
const body = {
|
|
200
|
+
template_uuid: args.template_uuid,
|
|
201
|
+
recipients: args.recipients,
|
|
202
|
+
};
|
|
203
|
+
if (args.name)
|
|
204
|
+
body.name = args.name;
|
|
205
|
+
if (args.tokens)
|
|
206
|
+
body.tokens = args.tokens;
|
|
207
|
+
if (args.fields)
|
|
208
|
+
body.fields = args.fields;
|
|
209
|
+
if (args.metadata)
|
|
210
|
+
body.metadata = args.metadata;
|
|
211
|
+
if (args.tags)
|
|
212
|
+
body.tags = args.tags;
|
|
213
|
+
if (args.folder_uuid)
|
|
214
|
+
body.folder_uuid = args.folder_uuid;
|
|
215
|
+
const result = await pandadocFetch('/documents', {
|
|
216
|
+
method: 'POST',
|
|
217
|
+
body: JSON.stringify(body),
|
|
218
|
+
});
|
|
219
|
+
return JSON.stringify({
|
|
220
|
+
ok: true,
|
|
221
|
+
document: formatDocumentCompact(result),
|
|
222
|
+
info: result.info_message || 'Document created. Poll get_document_status until status is "document.draft" before sending.',
|
|
223
|
+
});
|
|
224
|
+
}));
|
|
225
|
+
// ── upload_document ─────────────────────────────────────────────────
|
|
226
|
+
server.registerTool('upload_document', {
|
|
227
|
+
description: `Upload a PDF, DOCX, or RTF file to PandaDoc to create a new document.
|
|
228
|
+
|
|
229
|
+
The file is uploaded and converted into an interactive PandaDoc document.
|
|
230
|
+
After upload, the document status is 'document.uploaded' and transitions to 'document.draft' after processing (typically 1-5 seconds).
|
|
231
|
+
|
|
232
|
+
WORKFLOW:
|
|
233
|
+
1. Upload the file with this tool
|
|
234
|
+
2. Poll get_document_status until status is 'document.draft'
|
|
235
|
+
3. Then use send_document to send it for signing
|
|
236
|
+
|
|
237
|
+
COMMON MISTAKES:
|
|
238
|
+
- Don't try to send a document while it's still in 'document.uploaded' status — wait for 'document.draft'
|
|
239
|
+
- File must be under 50 MB
|
|
240
|
+
- Encrypted PDFs are not supported
|
|
241
|
+
|
|
242
|
+
RELATED TOOLS:
|
|
243
|
+
- get_document_status: Check when document is ready (status = 'document.draft')
|
|
244
|
+
- send_document: Send the processed document for e-signature`,
|
|
245
|
+
inputSchema: z.object({
|
|
246
|
+
file_path: z.string().min(1).describe('Absolute path to the PDF, DOCX, or RTF file to upload'),
|
|
247
|
+
name: z.string().optional().describe('Document name in PandaDoc (defaults to filename)'),
|
|
248
|
+
recipients: z.array(z.object({
|
|
249
|
+
email: z.string().describe('Recipient email address'),
|
|
250
|
+
first_name: z.string().optional().describe('Recipient first name'),
|
|
251
|
+
last_name: z.string().optional().describe('Recipient last name'),
|
|
252
|
+
role: z.string().optional().describe('Recipient role (e.g., "Client", "Signer")'),
|
|
253
|
+
})).optional().describe('List of document recipients (at least one required for sending)'),
|
|
254
|
+
parse_form_fields: z.boolean().optional().describe('If true, recognizes PDF form fields as PandaDoc fields. Default: false'),
|
|
255
|
+
tags: z.array(z.string()).optional().describe('Tags to apply to the document'),
|
|
256
|
+
folder_uuid: z.string().optional().describe('ID of the PandaDoc folder to store the document in'),
|
|
257
|
+
}),
|
|
258
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
259
|
+
}, withErrorHandling(async (args) => {
|
|
260
|
+
if (!isConfigured())
|
|
261
|
+
return noApiKeyError();
|
|
262
|
+
const resolvedPath = path.resolve(args.file_path);
|
|
263
|
+
// Validate file exists and check size
|
|
264
|
+
let fileInfo;
|
|
265
|
+
try {
|
|
266
|
+
fileInfo = fs.statSync(resolvedPath);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return JSON.stringify({
|
|
270
|
+
ok: false,
|
|
271
|
+
error: `File not found: ${resolvedPath}`,
|
|
272
|
+
resolution: 'Check the file path exists and is accessible.',
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
if (fileInfo.size > MAX_FILE_SIZE) {
|
|
276
|
+
return JSON.stringify({
|
|
277
|
+
ok: false,
|
|
278
|
+
error: `File too large (${(fileInfo.size / 1024 / 1024).toFixed(1)}MB). Maximum is 50MB.`,
|
|
279
|
+
resolution: 'Use a smaller file or compress it before uploading.',
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
// Validate file extension
|
|
283
|
+
const ext = path.extname(resolvedPath).toLowerCase();
|
|
284
|
+
if (!['.pdf', '.docx', '.rtf'].includes(ext)) {
|
|
285
|
+
return JSON.stringify({
|
|
286
|
+
ok: false,
|
|
287
|
+
error: `Unsupported file type: ${ext}. PandaDoc accepts PDF, DOCX, and RTF files.`,
|
|
288
|
+
resolution: 'Convert the file to PDF, DOCX, or RTF before uploading.',
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const fileBuffer = fs.readFileSync(resolvedPath);
|
|
292
|
+
const fileName = args.name || path.basename(resolvedPath);
|
|
293
|
+
// Build metadata JSON for the 'data' field
|
|
294
|
+
const data = { name: fileName };
|
|
295
|
+
if (args.recipients)
|
|
296
|
+
data.recipients = args.recipients;
|
|
297
|
+
if (args.tags)
|
|
298
|
+
data.tags = args.tags;
|
|
299
|
+
if (args.folder_uuid)
|
|
300
|
+
data.folder_uuid = args.folder_uuid;
|
|
301
|
+
if (args.parse_form_fields)
|
|
302
|
+
data.parse_form_fields = true;
|
|
303
|
+
const mimeTypes = {
|
|
304
|
+
'.pdf': 'application/pdf',
|
|
305
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
306
|
+
'.rtf': 'application/rtf',
|
|
307
|
+
};
|
|
308
|
+
const formData = new FormData();
|
|
309
|
+
formData.append('file', new Blob([fileBuffer], { type: mimeTypes[ext] || 'application/octet-stream' }), path.basename(resolvedPath));
|
|
310
|
+
formData.append('data', JSON.stringify(data));
|
|
311
|
+
// Upload — use pandadocFetch but with FormData body and no Content-Type (let browser set multipart boundary)
|
|
312
|
+
const key = (await import('../auth.js')).getApiKey();
|
|
313
|
+
const { PANDADOC_API_BASE, REQUEST_TIMEOUT_MS } = await import('../types.js');
|
|
314
|
+
const { PandaDocError } = await import('../types.js');
|
|
315
|
+
const useFormFields = args.parse_form_fields ? '&use_form_field_properties=true' : '';
|
|
316
|
+
const url = `${PANDADOC_API_BASE}/documents?upload${useFormFields}`;
|
|
317
|
+
let response;
|
|
318
|
+
try {
|
|
319
|
+
response = await fetch(url, {
|
|
320
|
+
method: 'POST',
|
|
321
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
322
|
+
headers: {
|
|
323
|
+
Authorization: `API-Key ${key}`,
|
|
324
|
+
},
|
|
325
|
+
body: formData,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch (error) {
|
|
329
|
+
if (error instanceof Error && error.name === 'TimeoutError') {
|
|
330
|
+
throw new PandaDocError('Upload to PandaDoc API timed out', 'TIMEOUT', 'The upload took too long. Try again or use a smaller file.');
|
|
331
|
+
}
|
|
332
|
+
throw error;
|
|
333
|
+
}
|
|
334
|
+
if (!response.ok) {
|
|
335
|
+
if (response.status === 429) {
|
|
336
|
+
throw new PandaDocError('Rate limited by PandaDoc API.', 'RATE_LIMITED', 'Wait a moment and try again.');
|
|
337
|
+
}
|
|
338
|
+
const errorText = await response.text().catch(() => 'Unknown error');
|
|
339
|
+
throw new PandaDocError(`PandaDoc upload error (${response.status}): ${errorText}`, 'UPLOAD_ERROR', 'Check the file and try again.');
|
|
340
|
+
}
|
|
341
|
+
const result = await response.json();
|
|
342
|
+
return JSON.stringify({
|
|
343
|
+
ok: true,
|
|
344
|
+
document: formatDocumentCompact(result),
|
|
345
|
+
info: result.info_message || 'Document uploaded. Poll get_document_status until status is "document.draft" before sending.',
|
|
346
|
+
});
|
|
347
|
+
}));
|
|
348
|
+
// ── send_document ───────────────────────────────────────────────────
|
|
349
|
+
server.registerTool('send_document', {
|
|
350
|
+
description: `Send a PandaDoc document to recipients for viewing/signing.
|
|
351
|
+
|
|
352
|
+
The document must be in 'document.draft' status before sending.
|
|
353
|
+
Optionally include a custom email message and subject line.
|
|
354
|
+
|
|
355
|
+
COMMON MISTAKES:
|
|
356
|
+
- Cannot send a document in 'document.uploaded' status — wait for 'document.draft'
|
|
357
|
+
- Cannot re-send a document that is already 'document.completed' or 'document.voided'
|
|
358
|
+
|
|
359
|
+
RELATED TOOLS:
|
|
360
|
+
- get_document_status: Verify document is in 'document.draft' before sending
|
|
361
|
+
- get_document_details: Review document content before sending`,
|
|
362
|
+
inputSchema: z.object({
|
|
363
|
+
document_id: z.string().min(1).describe('The document ID'),
|
|
364
|
+
message: z.string().optional().describe('Email body message sent to recipients with the document link'),
|
|
365
|
+
subject: z.string().optional().describe('Email subject line'),
|
|
366
|
+
silent: z.boolean().optional().describe('If true, suppresses email notifications to recipients. Default: false'),
|
|
367
|
+
}),
|
|
368
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
369
|
+
}, withErrorHandling(async (args) => {
|
|
370
|
+
if (!isConfigured())
|
|
371
|
+
return noApiKeyError();
|
|
372
|
+
const body = {};
|
|
373
|
+
if (args.message)
|
|
374
|
+
body.message = args.message;
|
|
375
|
+
if (args.subject)
|
|
376
|
+
body.subject = args.subject;
|
|
377
|
+
if (args.silent !== undefined)
|
|
378
|
+
body.silent = args.silent;
|
|
379
|
+
const result = await pandadocFetch(`/documents/${encodeURIComponent(args.document_id)}/send`, {
|
|
380
|
+
method: 'POST',
|
|
381
|
+
body: JSON.stringify(body),
|
|
382
|
+
});
|
|
383
|
+
return JSON.stringify({
|
|
384
|
+
ok: true,
|
|
385
|
+
document: {
|
|
386
|
+
id: result.id,
|
|
387
|
+
name: result.name,
|
|
388
|
+
status: result.status,
|
|
389
|
+
recipients: result.recipients,
|
|
390
|
+
},
|
|
391
|
+
message: 'Document sent successfully.',
|
|
392
|
+
});
|
|
393
|
+
}));
|
|
394
|
+
// ── download_document ───────────────────────────────────────────────
|
|
395
|
+
server.registerTool('download_document', {
|
|
396
|
+
description: `Download a PandaDoc document as a PDF file.
|
|
397
|
+
|
|
398
|
+
Returns the file path where the PDF has been saved. The document must be in a
|
|
399
|
+
completed or sent status to download.
|
|
400
|
+
|
|
401
|
+
RELATED TOOLS:
|
|
402
|
+
- get_document_status: Check document status before downloading
|
|
403
|
+
- list_documents: Find document IDs`,
|
|
404
|
+
inputSchema: z.object({
|
|
405
|
+
document_id: z.string().min(1).describe('The document ID'),
|
|
406
|
+
watermark_text: z.string().optional().describe('Optional watermark text to overlay on the PDF'),
|
|
407
|
+
watermark_color: z.string().optional().describe('Watermark color as HEX code (e.g., "#FF5733")'),
|
|
408
|
+
watermark_font_size: z.number().optional().describe('Watermark font size'),
|
|
409
|
+
watermark_opacity: z.number().optional().describe('Watermark opacity (0.0 to 1.0)'),
|
|
410
|
+
separate_files: z.boolean().optional().describe('If true, downloads as a zip archive with separate PDFs per section'),
|
|
411
|
+
}),
|
|
412
|
+
annotations: { readOnlyHint: true },
|
|
413
|
+
}, withErrorHandling(async (args) => {
|
|
414
|
+
if (!isConfigured())
|
|
415
|
+
return noApiKeyError();
|
|
416
|
+
const params = new URLSearchParams();
|
|
417
|
+
if (args.watermark_text)
|
|
418
|
+
params.set('watermark_text', args.watermark_text);
|
|
419
|
+
if (args.watermark_color)
|
|
420
|
+
params.set('watermark_color', args.watermark_color);
|
|
421
|
+
if (args.watermark_font_size)
|
|
422
|
+
params.set('watermark_font_size', String(args.watermark_font_size));
|
|
423
|
+
if (args.watermark_opacity)
|
|
424
|
+
params.set('watermark_opacity', String(args.watermark_opacity));
|
|
425
|
+
if (args.separate_files)
|
|
426
|
+
params.set('separate_files', 'true');
|
|
427
|
+
const queryStr = params.toString();
|
|
428
|
+
const downloadPath = `/documents/${encodeURIComponent(args.document_id)}/download${queryStr ? `?${queryStr}` : ''}`;
|
|
429
|
+
const response = await pandadocFetchRaw(downloadPath);
|
|
430
|
+
// Save the PDF to a temp file
|
|
431
|
+
const pdfBuffer = Buffer.from(await response.arrayBuffer());
|
|
432
|
+
const tmpDir = process.env.TMPDIR || process.env.TEMP || '/tmp';
|
|
433
|
+
const safeName = `pandadoc_${args.document_id.replace(/[^a-zA-Z0-9_-]/g, '_')}.pdf`;
|
|
434
|
+
const outputPath = path.join(tmpDir, safeName);
|
|
435
|
+
fs.writeFileSync(outputPath, pdfBuffer);
|
|
436
|
+
return JSON.stringify({
|
|
437
|
+
ok: true,
|
|
438
|
+
file_path: outputPath,
|
|
439
|
+
file_size: `${(pdfBuffer.length / 1024).toFixed(1)}KB`,
|
|
440
|
+
message: `Document downloaded to ${outputPath}`,
|
|
441
|
+
});
|
|
442
|
+
}));
|
|
443
|
+
}
|
|
444
|
+
//# sourceMappingURL=documents.js.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { pandadocFetch } from '../client.js';
|
|
3
|
+
import { withErrorHandling } from '../utils.js';
|
|
4
|
+
import { isConfigured } from '../auth.js';
|
|
5
|
+
function noApiKeyError() {
|
|
6
|
+
return JSON.stringify({
|
|
7
|
+
ok: false,
|
|
8
|
+
error: 'PandaDoc API key not configured',
|
|
9
|
+
resolution: 'To use PandaDoc, you need to configure an API key first.',
|
|
10
|
+
next_step: {
|
|
11
|
+
action: 'Ask the user for their PandaDoc API key, then call configure_pandadoc_api_key',
|
|
12
|
+
tool_to_call: 'configure_pandadoc_api_key',
|
|
13
|
+
tool_parameters: { api_key: '<user_provided_key>' },
|
|
14
|
+
get_key_from: 'PandaDoc Settings → API → Developer Dashboard. Requires Business or Enterprise plan.',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function paginationHint(count, page, pageSize) {
|
|
19
|
+
if (count < pageSize)
|
|
20
|
+
return `Showing all ${count} results.`;
|
|
21
|
+
return `Showing ${count} results (page ${page}). Use page=${page + 1} to see more.`;
|
|
22
|
+
}
|
|
23
|
+
export function registerTemplateTools(server) {
|
|
24
|
+
server.registerTool('list_templates', {
|
|
25
|
+
description: `List available PandaDoc templates.
|
|
26
|
+
|
|
27
|
+
Returns template IDs, names, and dates. Use template IDs with create_document_from_template.
|
|
28
|
+
|
|
29
|
+
RELATED TOOLS:
|
|
30
|
+
- create_document_from_template: Use a template ID to create a new document`,
|
|
31
|
+
inputSchema: z.object({
|
|
32
|
+
q: z.string().optional().describe('Search by template name'),
|
|
33
|
+
folder_uuid: z.string().optional().describe('Filter by folder ID'),
|
|
34
|
+
tag: z.array(z.string()).optional().describe('Filter by tags'),
|
|
35
|
+
count: z.number().min(1).max(100).default(50).describe('Results per page (default 50, max 100)'),
|
|
36
|
+
page: z.number().min(1).default(1).describe('Page number (starts at 1)'),
|
|
37
|
+
}),
|
|
38
|
+
annotations: { readOnlyHint: true },
|
|
39
|
+
}, withErrorHandling(async (args) => {
|
|
40
|
+
if (!isConfigured())
|
|
41
|
+
return noApiKeyError();
|
|
42
|
+
const params = new URLSearchParams();
|
|
43
|
+
params.set('count', String(args.count));
|
|
44
|
+
params.set('page', String(args.page));
|
|
45
|
+
if (args.q)
|
|
46
|
+
params.set('q', args.q);
|
|
47
|
+
if (args.folder_uuid)
|
|
48
|
+
params.set('folder_uuid', args.folder_uuid);
|
|
49
|
+
if (args.tag && Array.isArray(args.tag)) {
|
|
50
|
+
for (const t of args.tag) {
|
|
51
|
+
params.append('tag', t);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const result = await pandadocFetch(`/templates?${params.toString()}`);
|
|
55
|
+
const templates = (result.results || []).map((t) => ({
|
|
56
|
+
id: t.id,
|
|
57
|
+
name: t.name,
|
|
58
|
+
date_created: t.date_created,
|
|
59
|
+
date_modified: t.date_modified,
|
|
60
|
+
version: t.version,
|
|
61
|
+
}));
|
|
62
|
+
const hint = paginationHint(templates.length, args.page, args.count);
|
|
63
|
+
return JSON.stringify({ ok: true, templates, count: templates.length, pagination: hint });
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=templates.js.map
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export declare const REQUEST_TIMEOUT_MS = 30000;
|
|
2
|
+
export declare const PANDADOC_API_BASE = "https://api.pandadoc.com/public/v1";
|
|
3
|
+
export declare const MAX_FILE_SIZE: number;
|
|
4
|
+
export interface BridgeState {
|
|
5
|
+
port: number;
|
|
6
|
+
token: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class PandaDocError extends Error {
|
|
9
|
+
readonly code: string;
|
|
10
|
+
readonly resolution: string;
|
|
11
|
+
constructor(message: string, code: string, resolution: string);
|
|
12
|
+
}
|
|
13
|
+
export interface DocumentCompact {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
status: string;
|
|
17
|
+
date_created: string;
|
|
18
|
+
date_modified: string;
|
|
19
|
+
expiration_date: string | null;
|
|
20
|
+
version: string | null;
|
|
21
|
+
}
|
|
22
|
+
export interface DocumentDetails extends DocumentCompact {
|
|
23
|
+
date_completed: string | null;
|
|
24
|
+
date_sent: string | null;
|
|
25
|
+
created_by: Record<string, unknown>;
|
|
26
|
+
template: Record<string, unknown> | null;
|
|
27
|
+
recipients: Record<string, unknown>[];
|
|
28
|
+
fields: Record<string, unknown>[];
|
|
29
|
+
tokens: Record<string, unknown>[];
|
|
30
|
+
metadata: Record<string, unknown>;
|
|
31
|
+
tags: string[];
|
|
32
|
+
grand_total: Record<string, unknown> | null;
|
|
33
|
+
linked_objects: Record<string, unknown>[];
|
|
34
|
+
}
|
|
35
|
+
export interface DocumentListResponse {
|
|
36
|
+
results: Record<string, unknown>[];
|
|
37
|
+
}
|
|
38
|
+
export interface TemplateListResponse {
|
|
39
|
+
results: Record<string, unknown>[];
|
|
40
|
+
}
|
|
41
|
+
export interface DocumentCreateResponse {
|
|
42
|
+
id: string;
|
|
43
|
+
name: string;
|
|
44
|
+
status: string;
|
|
45
|
+
date_created: string;
|
|
46
|
+
date_modified: string;
|
|
47
|
+
expiration_date: string | null;
|
|
48
|
+
version: string | null;
|
|
49
|
+
uuid: string;
|
|
50
|
+
links: Array<{
|
|
51
|
+
rel: string;
|
|
52
|
+
href: string;
|
|
53
|
+
type: string;
|
|
54
|
+
}>;
|
|
55
|
+
info_message: string;
|
|
56
|
+
}
|
|
57
|
+
export interface DocumentSendResponse {
|
|
58
|
+
id: string;
|
|
59
|
+
name: string;
|
|
60
|
+
status: string;
|
|
61
|
+
date_created: string;
|
|
62
|
+
date_modified: string;
|
|
63
|
+
recipients: Array<Record<string, unknown>>;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const REQUEST_TIMEOUT_MS = 30_000;
|
|
2
|
+
export const PANDADOC_API_BASE = 'https://api.pandadoc.com/public/v1';
|
|
3
|
+
export const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50 MB
|
|
4
|
+
export class PandaDocError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
resolution;
|
|
7
|
+
constructor(message, code, resolution) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.resolution = resolution;
|
|
11
|
+
this.name = 'PandaDocError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
type ToolHandler<T> = (args: T, extra: unknown) => Promise<CallToolResult>;
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a tool handler with standard error handling.
|
|
5
|
+
*
|
|
6
|
+
* - On success: returns the string result as a text content block.
|
|
7
|
+
* - On PandaDocError: returns a structured JSON error with code and resolution.
|
|
8
|
+
* - On unknown error: returns a generic error message.
|
|
9
|
+
*
|
|
10
|
+
* Secrets are never exposed in error messages.
|
|
11
|
+
*/
|
|
12
|
+
export declare function withErrorHandling<T>(fn: (args: T, extra: unknown) => Promise<string>): ToolHandler<T>;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { PandaDocError } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a tool handler with standard error handling.
|
|
4
|
+
*
|
|
5
|
+
* - On success: returns the string result as a text content block.
|
|
6
|
+
* - On PandaDocError: returns a structured JSON error with code and resolution.
|
|
7
|
+
* - On unknown error: returns a generic error message.
|
|
8
|
+
*
|
|
9
|
+
* Secrets are never exposed in error messages.
|
|
10
|
+
*/
|
|
11
|
+
export function withErrorHandling(fn) {
|
|
12
|
+
return async (args, extra) => {
|
|
13
|
+
try {
|
|
14
|
+
const result = await fn(args, extra);
|
|
15
|
+
return { content: [{ type: 'text', text: result }] };
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (error instanceof PandaDocError) {
|
|
19
|
+
return {
|
|
20
|
+
content: [
|
|
21
|
+
{
|
|
22
|
+
type: 'text',
|
|
23
|
+
text: JSON.stringify({
|
|
24
|
+
ok: false,
|
|
25
|
+
error: error.message,
|
|
26
|
+
code: error.code,
|
|
27
|
+
resolution: error.resolution,
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
isError: true,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
35
|
+
return {
|
|
36
|
+
content: [{ type: 'text', text: JSON.stringify({ ok: false, error: errorMessage }) }],
|
|
37
|
+
isError: true,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=utils.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mindstone-engineering/mcp-server-pandadoc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PandaDoc document automation MCP server for Model Context Protocol hosts",
|
|
5
|
+
"license": "FSL-1.1-MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"mcp-server-pandadoc": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"!dist/**/*.map"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/nspr-io/mcp-servers.git",
|
|
17
|
+
"directory": "connectors/pandadoc"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/nspr-io/mcp-servers/tree/main/connectors/pandadoc",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc && shx chmod +x dist/index.js",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"watch": "tsc --watch",
|
|
27
|
+
"start": "node dist/index.js",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest",
|
|
30
|
+
"test:coverage": "vitest run --coverage"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
34
|
+
"zod": "^3.23.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@mindstone-engineering/mcp-test-harness": "file:../../test-harness",
|
|
38
|
+
"@types/node": "^22",
|
|
39
|
+
"@vitest/coverage-v8": "^4.1.3",
|
|
40
|
+
"msw": "^2.13.2",
|
|
41
|
+
"shx": "^0.3.4",
|
|
42
|
+
"typescript": "^5.8.2",
|
|
43
|
+
"vitest": "^4.1.3"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
47
|
+
}
|
|
48
|
+
}
|