Skip to main content
Glama
users.ts6.79 kB
// src/tools/users.ts import { z } from "zod"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { ghostApiClient } from "../ghostApi.js"; import { getGhostApiConfig, generateGhostAdminToken } from "../ghostApi.js"; import axios from 'axios'; // Parameter schemas as ZodRawShape (object literals) const browseParams = { filter: z.string().optional(), limit: z.number().optional(), page: z.number().optional(), order: z.string().optional(), }; const readParams = { id: z.string().optional(), email: z.string().optional(), slug: z.string().optional(), }; const editParams = { id: z.string(), name: z.string().optional(), email: z.string().optional(), slug: z.string().optional(), bio: z.string().optional(), website: z.string().optional(), location: z.string().optional(), facebook: z.string().optional(), twitter: z.string().optional(), // Add more fields as needed }; const deleteParams = { id: z.string(), }; export function registerUserTools(server: McpServer) { // Browse users server.tool( "users_browse", browseParams, async (args, _extra) => { const config = getGhostApiConfig(); if (!config) { return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] }; } try { const token = generateGhostAdminToken(config.key); const url = `${config.url}/ghost/api/admin/users/`; const headers = { 'Authorization': `Ghost ${token}` }; const response = await axios.get(url, { params: args, headers }); return { content: [ { type: "text", text: JSON.stringify(response.data.users, null, 2), }, ], }; } catch (error: any) { const status = error?.response?.status ?? error?.status ?? "unknown"; const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error); const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2); return { isError: true, content: [ { type: "text", text: `users_browse failed. status=${status}\n${bodyText}`, }, ], }; } } ); // Read user server.tool( "users_read", readParams, async (args, _extra) => { const config = getGhostApiConfig(); if (!config) { return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] }; } try { const token = generateGhostAdminToken(config.key); const url = `${config.url}/ghost/api/admin/users/${args.id || `slug/${args.slug}`}/`; const headers = { 'Authorization': `Ghost ${token}` }; const response = await axios.get(url, { headers }); return { content: [ { type: "text", text: JSON.stringify(response.data.users[0], null, 2), }, ], }; } catch (error: any) { const status = error?.response?.status ?? error?.status ?? "unknown"; const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error); const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2); return { isError: true, content: [ { type: "text", text: `users_read failed. status=${status}\n${bodyText}`, }, ], }; } } ); // Edit user server.tool( "users_edit", editParams, async (args, _extra) => { const config = getGhostApiConfig(); if (!config) { return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] }; } try { const token = generateGhostAdminToken(config.key); const url = `${config.url}/ghost/api/admin/users/${args.id}/`; const headers = { 'Authorization': `Ghost ${token}` }; const response = await axios.put(url, { users: [args] }, { headers }); return { content: [ { type: "text", text: JSON.stringify(response.data.users[0], null, 2), }, ], }; } catch (error: any) { const status = error?.response?.status ?? error?.status ?? "unknown"; const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error); const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2); return { isError: true, content: [ { type: "text", text: `users_edit failed. status=${status}\n${bodyText}`, }, ], }; } } ); // Delete user server.tool( "users_delete", deleteParams, async (args, _extra) => { const config = getGhostApiConfig(); if (!config) { return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] }; } try { const token = generateGhostAdminToken(config.key); const url = `${config.url}/ghost/api/admin/users/${args.id}/`; const headers = { 'Authorization': `Ghost ${token}` }; await axios.delete(url, { headers }); return { content: [ { type: "text", text: `User with id ${args.id} deleted.`, }, ], }; } catch (error: any) { const status = error?.response?.status ?? error?.status ?? "unknown"; const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error); const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2); return { isError: true, content: [ { type: "text", text: `users_delete failed. status=${status}\n${bodyText}`, }, ], }; } } ); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hithereiamaliff/mcp-ghostcms'

If you have feedback or need assistance with the MCP directory API, please join our Discord server