@postman/postman-mcp-server
Version:
Postman MCP Server — connect AI agents (Claude Code, Cursor, VS Code Copilot, Gemini CLI) to your Postman collections, specifications, and environments via Model Context Protocol (MCP)
59 lines (58 loc) • 2.4 kB
JavaScript
import { z } from 'zod';
import { ContentType } from '../clients/postman.js';
import { asMcpError, McpError } from './utils/toolHelpers.js';
export const method = 'generateSpecFromCollection';
export const title = 'Generate spec from collection';
export const description = 'Generates an OpenAPI 2.0, 3.0, or 3.1 specification for the given collection. The response contains a polling link to the task status.';
export const parameters = z.object({
collectionUid: z.string().describe("The collection's unique ID."),
elementType: z.literal('spec').describe('The `spec` value.'),
name: z.string().describe("The API specification's name."),
type: z
.preprocess((v) => (typeof v === 'string' ? v.toUpperCase() : v), z.enum(['OPENAPI:2.0', 'OPENAPI:3.0', 'OPENAPI:3.1']))
.describe("The specification's type."),
format: z
.preprocess((v) => (typeof v === 'string' ? v.toUpperCase() : v), z.enum(['JSON', 'YAML']))
.describe('The format of the API specification.'),
});
export const annotations = {
title: 'Generate spec from collection',
readOnlyHint: false,
openWorldHint: true,
destructiveHint: false,
idempotentHint: false,
};
export async function handler(args, extra) {
try {
const endpoint = `/collections/${encodeURIComponent(String(args.collectionUid))}/generations/${encodeURIComponent(String(args.elementType))}`;
const query = new URLSearchParams();
const url = query.toString() ? `${endpoint}?${query.toString()}` : endpoint;
const bodyPayload = {};
if (args.name !== undefined)
bodyPayload.name = args.name;
if (args.type !== undefined)
bodyPayload.type = args.type;
if (args.format !== undefined)
bodyPayload.format = args.format;
const options = {
body: JSON.stringify(bodyPayload),
contentType: ContentType.Json,
headers: extra.headers,
};
const result = await extra.client.post(url, options);
return {
content: [
{
type: 'text',
text: `${typeof result === 'string' ? result : JSON.stringify(result, null, 2)}`,
},
],
};
}
catch (e) {
if (e instanceof McpError) {
throw e;
}
throw asMcpError(e);
}
}