@jgardner04/ghost-mcp-server 1.1.13 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jgardner04/ghost-mcp-server",
3
- "version": "1.1.13",
3
+ "version": "1.2.0",
4
4
  "description": "A Model Context Protocol (MCP) server for interacting with Ghost CMS via the Admin API",
5
5
  "author": "Jonathan Gardner",
6
6
  "type": "module",
@@ -0,0 +1,440 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+
3
+ // Mock the McpServer to capture tool registrations
4
+ const mockTools = new Map();
5
+
6
+ vi.mock('@modelcontextprotocol/sdk/server/mcp.js', () => {
7
+ return {
8
+ McpServer: class MockMcpServer {
9
+ constructor(config) {
10
+ this.config = config;
11
+ }
12
+
13
+ tool(name, description, schema, handler) {
14
+ mockTools.set(name, { name, description, schema, handler });
15
+ }
16
+
17
+ connect(_transport) {
18
+ return Promise.resolve();
19
+ }
20
+ },
21
+ };
22
+ });
23
+
24
+ vi.mock('@modelcontextprotocol/sdk/server/stdio.js', () => {
25
+ return {
26
+ StdioServerTransport: class MockStdioServerTransport {},
27
+ };
28
+ });
29
+
30
+ // Mock dotenv
31
+ vi.mock('dotenv', () => ({
32
+ default: { config: vi.fn() },
33
+ }));
34
+
35
+ // Mock crypto
36
+ vi.mock('crypto', () => ({
37
+ default: { randomUUID: vi.fn().mockReturnValue('test-uuid-1234') },
38
+ }));
39
+
40
+ // Mock services - will be lazy loaded
41
+ const mockGetPosts = vi.fn();
42
+ const mockGetPost = vi.fn();
43
+ const mockGetTags = vi.fn();
44
+ const mockCreateTag = vi.fn();
45
+ const mockUploadImage = vi.fn();
46
+ const mockCreatePostService = vi.fn();
47
+ const mockProcessImage = vi.fn();
48
+ const mockValidateImageUrl = vi.fn();
49
+ const mockCreateSecureAxiosConfig = vi.fn();
50
+
51
+ vi.mock('../services/ghostService.js', () => ({
52
+ getPosts: (...args) => mockGetPosts(...args),
53
+ getPost: (...args) => mockGetPost(...args),
54
+ getTags: (...args) => mockGetTags(...args),
55
+ createTag: (...args) => mockCreateTag(...args),
56
+ uploadImage: (...args) => mockUploadImage(...args),
57
+ }));
58
+
59
+ vi.mock('../services/postService.js', () => ({
60
+ createPostService: (...args) => mockCreatePostService(...args),
61
+ }));
62
+
63
+ vi.mock('../services/imageProcessingService.js', () => ({
64
+ processImage: (...args) => mockProcessImage(...args),
65
+ }));
66
+
67
+ vi.mock('../utils/urlValidator.js', () => ({
68
+ validateImageUrl: (...args) => mockValidateImageUrl(...args),
69
+ createSecureAxiosConfig: (...args) => mockCreateSecureAxiosConfig(...args),
70
+ }));
71
+
72
+ // Mock axios
73
+ const mockAxios = vi.fn();
74
+ vi.mock('axios', () => ({
75
+ default: (...args) => mockAxios(...args),
76
+ }));
77
+
78
+ // Mock fs
79
+ const mockUnlink = vi.fn((path, cb) => cb(null));
80
+ const mockCreateWriteStream = vi.fn();
81
+ vi.mock('fs', () => ({
82
+ default: {
83
+ unlink: (...args) => mockUnlink(...args),
84
+ createWriteStream: (...args) => mockCreateWriteStream(...args),
85
+ },
86
+ }));
87
+
88
+ // Mock os
89
+ vi.mock('os', () => ({
90
+ default: { tmpdir: vi.fn().mockReturnValue('/tmp') },
91
+ }));
92
+
93
+ // Mock path
94
+ vi.mock('path', async () => {
95
+ const actual = await vi.importActual('path');
96
+ return {
97
+ default: actual,
98
+ ...actual,
99
+ };
100
+ });
101
+
102
+ describe('mcp_server_improved - ghost_get_posts tool', () => {
103
+ beforeEach(async () => {
104
+ vi.clearAllMocks();
105
+ // Don't clear mockTools - they're registered once on module load
106
+ // Import the module to register tools (only first time)
107
+ if (mockTools.size === 0) {
108
+ await import('../mcp_server_improved.js');
109
+ }
110
+ });
111
+
112
+ it('should register ghost_get_posts tool', () => {
113
+ expect(mockTools.has('ghost_get_posts')).toBe(true);
114
+ });
115
+
116
+ it('should have correct schema with all optional parameters', () => {
117
+ const tool = mockTools.get('ghost_get_posts');
118
+ expect(tool).toBeDefined();
119
+ expect(tool.description).toContain('posts');
120
+ expect(tool.schema).toBeDefined();
121
+ expect(tool.schema.limit).toBeDefined();
122
+ expect(tool.schema.page).toBeDefined();
123
+ expect(tool.schema.status).toBeDefined();
124
+ expect(tool.schema.include).toBeDefined();
125
+ expect(tool.schema.filter).toBeDefined();
126
+ expect(tool.schema.order).toBeDefined();
127
+ });
128
+
129
+ it('should retrieve posts with default options', async () => {
130
+ const mockPosts = [
131
+ { id: '1', title: 'Post 1', slug: 'post-1', status: 'published' },
132
+ { id: '2', title: 'Post 2', slug: 'post-2', status: 'draft' },
133
+ ];
134
+ mockGetPosts.mockResolvedValue(mockPosts);
135
+
136
+ const tool = mockTools.get('ghost_get_posts');
137
+ const result = await tool.handler({});
138
+
139
+ expect(mockGetPosts).toHaveBeenCalledWith({});
140
+ expect(result.content[0].text).toContain('Post 1');
141
+ expect(result.content[0].text).toContain('Post 2');
142
+ });
143
+
144
+ it('should pass limit and page parameters', async () => {
145
+ const mockPosts = [{ id: '1', title: 'Post 1', slug: 'post-1' }];
146
+ mockGetPosts.mockResolvedValue(mockPosts);
147
+
148
+ const tool = mockTools.get('ghost_get_posts');
149
+ await tool.handler({ limit: 10, page: 2 });
150
+
151
+ expect(mockGetPosts).toHaveBeenCalledWith({ limit: 10, page: 2 });
152
+ });
153
+
154
+ it('should validate limit is between 1 and 100', () => {
155
+ const tool = mockTools.get('ghost_get_posts');
156
+ const schema = tool.schema;
157
+
158
+ // Test that limit schema exists and has proper validation
159
+ expect(schema.limit).toBeDefined();
160
+ expect(() => schema.limit.parse(0)).toThrow();
161
+ expect(() => schema.limit.parse(101)).toThrow();
162
+ expect(schema.limit.parse(50)).toBe(50);
163
+ });
164
+
165
+ it('should validate page is at least 1', () => {
166
+ const tool = mockTools.get('ghost_get_posts');
167
+ const schema = tool.schema;
168
+
169
+ expect(schema.page).toBeDefined();
170
+ expect(() => schema.page.parse(0)).toThrow();
171
+ expect(schema.page.parse(1)).toBe(1);
172
+ });
173
+
174
+ it('should pass status filter', async () => {
175
+ const mockPosts = [{ id: '1', title: 'Published Post', status: 'published' }];
176
+ mockGetPosts.mockResolvedValue(mockPosts);
177
+
178
+ const tool = mockTools.get('ghost_get_posts');
179
+ await tool.handler({ status: 'published' });
180
+
181
+ expect(mockGetPosts).toHaveBeenCalledWith({ status: 'published' });
182
+ });
183
+
184
+ it('should validate status enum values', () => {
185
+ const tool = mockTools.get('ghost_get_posts');
186
+ const schema = tool.schema;
187
+
188
+ expect(schema.status).toBeDefined();
189
+ expect(() => schema.status.parse('invalid')).toThrow();
190
+ expect(schema.status.parse('published')).toBe('published');
191
+ expect(schema.status.parse('draft')).toBe('draft');
192
+ expect(schema.status.parse('scheduled')).toBe('scheduled');
193
+ expect(schema.status.parse('all')).toBe('all');
194
+ });
195
+
196
+ it('should pass include parameter', async () => {
197
+ const mockPosts = [
198
+ {
199
+ id: '1',
200
+ title: 'Post with tags',
201
+ tags: [{ name: 'tech' }],
202
+ authors: [{ name: 'John' }],
203
+ },
204
+ ];
205
+ mockGetPosts.mockResolvedValue(mockPosts);
206
+
207
+ const tool = mockTools.get('ghost_get_posts');
208
+ await tool.handler({ include: 'tags,authors' });
209
+
210
+ expect(mockGetPosts).toHaveBeenCalledWith({ include: 'tags,authors' });
211
+ });
212
+
213
+ it('should pass filter parameter (NQL)', async () => {
214
+ const mockPosts = [{ id: '1', title: 'Featured Post', featured: true }];
215
+ mockGetPosts.mockResolvedValue(mockPosts);
216
+
217
+ const tool = mockTools.get('ghost_get_posts');
218
+ await tool.handler({ filter: 'featured:true' });
219
+
220
+ expect(mockGetPosts).toHaveBeenCalledWith({ filter: 'featured:true' });
221
+ });
222
+
223
+ it('should pass order parameter', async () => {
224
+ const mockPosts = [
225
+ { id: '1', title: 'Newest', published_at: '2025-12-10' },
226
+ { id: '2', title: 'Older', published_at: '2025-12-01' },
227
+ ];
228
+ mockGetPosts.mockResolvedValue(mockPosts);
229
+
230
+ const tool = mockTools.get('ghost_get_posts');
231
+ await tool.handler({ order: 'published_at DESC' });
232
+
233
+ expect(mockGetPosts).toHaveBeenCalledWith({ order: 'published_at DESC' });
234
+ });
235
+
236
+ it('should pass all parameters combined', async () => {
237
+ const mockPosts = [{ id: '1', title: 'Test Post' }];
238
+ mockGetPosts.mockResolvedValue(mockPosts);
239
+
240
+ const tool = mockTools.get('ghost_get_posts');
241
+ await tool.handler({
242
+ limit: 20,
243
+ page: 1,
244
+ status: 'published',
245
+ include: 'tags,authors',
246
+ filter: 'featured:true',
247
+ order: 'published_at DESC',
248
+ });
249
+
250
+ expect(mockGetPosts).toHaveBeenCalledWith({
251
+ limit: 20,
252
+ page: 1,
253
+ status: 'published',
254
+ include: 'tags,authors',
255
+ filter: 'featured:true',
256
+ order: 'published_at DESC',
257
+ });
258
+ });
259
+
260
+ it('should handle errors from ghostService', async () => {
261
+ mockGetPosts.mockRejectedValue(new Error('Ghost API error'));
262
+
263
+ const tool = mockTools.get('ghost_get_posts');
264
+ const result = await tool.handler({});
265
+
266
+ expect(result.isError).toBe(true);
267
+ expect(result.content[0].text).toContain('Ghost API error');
268
+ });
269
+
270
+ it('should return formatted JSON response', async () => {
271
+ const mockPosts = [
272
+ {
273
+ id: '1',
274
+ title: 'Test Post',
275
+ slug: 'test-post',
276
+ html: '<p>Content</p>',
277
+ status: 'published',
278
+ },
279
+ ];
280
+ mockGetPosts.mockResolvedValue(mockPosts);
281
+
282
+ const tool = mockTools.get('ghost_get_posts');
283
+ const result = await tool.handler({});
284
+
285
+ expect(result.content).toBeDefined();
286
+ expect(result.content[0].type).toBe('text');
287
+ expect(result.content[0].text).toContain('"id": "1"');
288
+ expect(result.content[0].text).toContain('"title": "Test Post"');
289
+ });
290
+ });
291
+
292
+ describe('mcp_server_improved - ghost_get_post tool', () => {
293
+ beforeEach(async () => {
294
+ vi.clearAllMocks();
295
+ // Don't clear mockTools - they're registered once on module load
296
+ if (mockTools.size === 0) {
297
+ await import('../mcp_server_improved.js');
298
+ }
299
+ });
300
+
301
+ it('should register ghost_get_post tool', () => {
302
+ expect(mockTools.has('ghost_get_post')).toBe(true);
303
+ });
304
+
305
+ it('should have correct schema requiring one of id or slug', () => {
306
+ const tool = mockTools.get('ghost_get_post');
307
+ expect(tool).toBeDefined();
308
+ expect(tool.description).toContain('post');
309
+ expect(tool.schema).toBeDefined();
310
+ expect(tool.schema.id).toBeDefined();
311
+ expect(tool.schema.slug).toBeDefined();
312
+ expect(tool.schema.include).toBeDefined();
313
+ });
314
+
315
+ it('should retrieve post by ID', async () => {
316
+ const mockPost = {
317
+ id: '123',
318
+ title: 'Test Post',
319
+ slug: 'test-post',
320
+ html: '<p>Content</p>',
321
+ status: 'published',
322
+ };
323
+ mockGetPost.mockResolvedValue(mockPost);
324
+
325
+ const tool = mockTools.get('ghost_get_post');
326
+ const result = await tool.handler({ id: '123' });
327
+
328
+ expect(mockGetPost).toHaveBeenCalledWith('123', {});
329
+ expect(result.content[0].text).toContain('"id": "123"');
330
+ expect(result.content[0].text).toContain('"title": "Test Post"');
331
+ });
332
+
333
+ it('should retrieve post by slug', async () => {
334
+ const mockPost = {
335
+ id: '123',
336
+ title: 'Test Post',
337
+ slug: 'test-post',
338
+ html: '<p>Content</p>',
339
+ status: 'published',
340
+ };
341
+ mockGetPost.mockResolvedValue(mockPost);
342
+
343
+ const tool = mockTools.get('ghost_get_post');
344
+ const result = await tool.handler({ slug: 'test-post' });
345
+
346
+ expect(mockGetPost).toHaveBeenCalledWith('slug/test-post', {});
347
+ expect(result.content[0].text).toContain('"title": "Test Post"');
348
+ });
349
+
350
+ it('should pass include parameter with ID', async () => {
351
+ const mockPost = {
352
+ id: '123',
353
+ title: 'Post with relations',
354
+ tags: [{ name: 'tech' }],
355
+ authors: [{ name: 'John' }],
356
+ };
357
+ mockGetPost.mockResolvedValue(mockPost);
358
+
359
+ const tool = mockTools.get('ghost_get_post');
360
+ await tool.handler({ id: '123', include: 'tags,authors' });
361
+
362
+ expect(mockGetPost).toHaveBeenCalledWith('123', { include: 'tags,authors' });
363
+ });
364
+
365
+ it('should pass include parameter with slug', async () => {
366
+ const mockPost = {
367
+ id: '123',
368
+ title: 'Post with relations',
369
+ slug: 'test-post',
370
+ tags: [{ name: 'tech' }],
371
+ };
372
+ mockGetPost.mockResolvedValue(mockPost);
373
+
374
+ const tool = mockTools.get('ghost_get_post');
375
+ await tool.handler({ slug: 'test-post', include: 'tags' });
376
+
377
+ expect(mockGetPost).toHaveBeenCalledWith('slug/test-post', { include: 'tags' });
378
+ });
379
+
380
+ it('should prefer ID over slug when both provided', async () => {
381
+ const mockPost = { id: '123', title: 'Test Post', slug: 'test-post' };
382
+ mockGetPost.mockResolvedValue(mockPost);
383
+
384
+ const tool = mockTools.get('ghost_get_post');
385
+ await tool.handler({ id: '123', slug: 'wrong-slug' });
386
+
387
+ expect(mockGetPost).toHaveBeenCalledWith('123', {});
388
+ });
389
+
390
+ it('should handle not found errors', async () => {
391
+ mockGetPost.mockRejectedValue(new Error('Post not found'));
392
+
393
+ const tool = mockTools.get('ghost_get_post');
394
+ const result = await tool.handler({ id: 'nonexistent' });
395
+
396
+ expect(result.isError).toBe(true);
397
+ expect(result.content[0].text).toContain('Post not found');
398
+ });
399
+
400
+ it('should handle errors from ghostService', async () => {
401
+ mockGetPost.mockRejectedValue(new Error('Ghost API error'));
402
+
403
+ const tool = mockTools.get('ghost_get_post');
404
+ const result = await tool.handler({ slug: 'test' });
405
+
406
+ expect(result.isError).toBe(true);
407
+ expect(result.content[0].text).toContain('Ghost API error');
408
+ });
409
+
410
+ it('should return formatted JSON response', async () => {
411
+ const mockPost = {
412
+ id: '1',
413
+ uuid: 'uuid-123',
414
+ title: 'Test Post',
415
+ slug: 'test-post',
416
+ html: '<p>Content</p>',
417
+ status: 'published',
418
+ created_at: '2025-12-10T00:00:00.000Z',
419
+ updated_at: '2025-12-10T00:00:00.000Z',
420
+ };
421
+ mockGetPost.mockResolvedValue(mockPost);
422
+
423
+ const tool = mockTools.get('ghost_get_post');
424
+ const result = await tool.handler({ id: '1' });
425
+
426
+ expect(result.content).toBeDefined();
427
+ expect(result.content[0].type).toBe('text');
428
+ expect(result.content[0].text).toContain('"id": "1"');
429
+ expect(result.content[0].text).toContain('"title": "Test Post"');
430
+ expect(result.content[0].text).toContain('"status": "published"');
431
+ });
432
+
433
+ it('should handle validation error when neither id nor slug provided', async () => {
434
+ const tool = mockTools.get('ghost_get_post');
435
+ const result = await tool.handler({});
436
+
437
+ expect(result.isError).toBe(true);
438
+ expect(result.content[0].text).toContain('Either id or slug is required');
439
+ });
440
+ });
@@ -272,6 +272,110 @@ server.tool(
272
272
  }
273
273
  );
274
274
 
275
+ // Get Posts Tool
276
+ server.tool(
277
+ 'ghost_get_posts',
278
+ 'Retrieves a list of posts from Ghost CMS with pagination, filtering, and sorting options.',
279
+ {
280
+ limit: z
281
+ .number()
282
+ .min(1)
283
+ .max(100)
284
+ .optional()
285
+ .describe('Number of posts to retrieve (1-100). Default is 15.'),
286
+ page: z.number().min(1).optional().describe('Page number for pagination. Default is 1.'),
287
+ status: z
288
+ .enum(['published', 'draft', 'scheduled', 'all'])
289
+ .optional()
290
+ .describe('Filter posts by status. Options: published, draft, scheduled, all.'),
291
+ include: z
292
+ .string()
293
+ .optional()
294
+ .describe('Comma-separated list of relations to include (e.g., "tags,authors").'),
295
+ filter: z
296
+ .string()
297
+ .optional()
298
+ .describe('Ghost NQL filter string for advanced filtering (e.g., "featured:true").'),
299
+ order: z
300
+ .string()
301
+ .optional()
302
+ .describe('Sort order for results (e.g., "published_at DESC", "title ASC").'),
303
+ },
304
+ async (input) => {
305
+ console.error(`Executing tool: ghost_get_posts`);
306
+ try {
307
+ await loadServices();
308
+
309
+ // Build options object with provided parameters
310
+ const options = {};
311
+ if (input.limit !== undefined) options.limit = input.limit;
312
+ if (input.page !== undefined) options.page = input.page;
313
+ if (input.status !== undefined) options.status = input.status;
314
+ if (input.include !== undefined) options.include = input.include;
315
+ if (input.filter !== undefined) options.filter = input.filter;
316
+ if (input.order !== undefined) options.order = input.order;
317
+
318
+ const posts = await ghostService.getPosts(options);
319
+ console.error(`Retrieved ${posts.length} posts from Ghost.`);
320
+
321
+ return {
322
+ content: [{ type: 'text', text: JSON.stringify(posts, null, 2) }],
323
+ };
324
+ } catch (error) {
325
+ console.error(`Error in ghost_get_posts:`, error);
326
+ return {
327
+ content: [{ type: 'text', text: `Error retrieving posts: ${error.message}` }],
328
+ isError: true,
329
+ };
330
+ }
331
+ }
332
+ );
333
+
334
+ // Get Post Tool
335
+ server.tool(
336
+ 'ghost_get_post',
337
+ 'Retrieves a single post from Ghost CMS by ID or slug.',
338
+ {
339
+ id: z.string().optional().describe('The ID of the post to retrieve.'),
340
+ slug: z.string().optional().describe('The slug of the post to retrieve.'),
341
+ include: z
342
+ .string()
343
+ .optional()
344
+ .describe('Comma-separated list of relations to include (e.g., "tags,authors").'),
345
+ },
346
+ async (input) => {
347
+ console.error(`Executing tool: ghost_get_post`);
348
+ try {
349
+ // Validate that at least one of id or slug is provided
350
+ if (!input.id && !input.slug) {
351
+ throw new Error('Either id or slug is required to retrieve a post');
352
+ }
353
+
354
+ await loadServices();
355
+
356
+ // Build options object
357
+ const options = {};
358
+ if (input.include !== undefined) options.include = input.include;
359
+
360
+ // Determine identifier (prefer ID over slug)
361
+ const identifier = input.id || `slug/${input.slug}`;
362
+
363
+ const post = await ghostService.getPost(identifier, options);
364
+ console.error(`Retrieved post: ${post.title} (ID: ${post.id})`);
365
+
366
+ return {
367
+ content: [{ type: 'text', text: JSON.stringify(post, null, 2) }],
368
+ };
369
+ } catch (error) {
370
+ console.error(`Error in ghost_get_post:`, error);
371
+ return {
372
+ content: [{ type: 'text', text: `Error retrieving post: ${error.message}` }],
373
+ isError: true,
374
+ };
375
+ }
376
+ }
377
+ );
378
+
275
379
  // --- Main Entry Point ---
276
380
 
277
381
  async function main() {
@@ -282,7 +386,7 @@ async function main() {
282
386
 
283
387
  console.error('Ghost MCP Server running on stdio transport');
284
388
  console.error(
285
- 'Available tools: ghost_get_tags, ghost_create_tag, ghost_upload_image, ghost_create_post'
389
+ 'Available tools: ghost_get_tags, ghost_create_tag, ghost_upload_image, ghost_create_post, ghost_get_posts, ghost_get_post'
286
390
  );
287
391
  }
288
392