#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { type CallToolRequest, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import fetch from 'node-fetch';
const server = new Server(
{
name: 'medium-mcp',
version: '1.0.0',
},
{
capabilities: { tools: {} },
},
);
server.setRequestHandler(CallToolRequestSchema, async (req: CallToolRequest) => {
if (req.params.name === 'publishPost') {
const { title, content, tags, canonicalUrl, publishStatus } = req.params.arguments as {
title: string;
content: string;
tags?: string[];
canonicalUrl?: string;
publishStatus?: 'draft' | 'unlisted' | 'public';
};
const me = (await fetch('https://api.medium.com/v1/me', {
headers: { Authorization: `Bearer ${process.env.MEDIUM_TOKEN}` },
}).then(res => res.json())) as { data: { id: string } };
const userId = me.data.id;
const post = await fetch(`https://api.medium.com/v1/users/${userId}/posts`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.MEDIUM_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
title,
contentFormat: 'html',
content,
tags,
canonicalUrl,
publishStatus: publishStatus || 'draft',
notifyFollowers: publishStatus === 'public',
}),
}).then(res => res.json());
return {
content: [{ type: 'text', text: JSON.stringify(post, null, 2) }],
};
}
throw new Error(`Unknown tool: ${req.params.name}`);
});
// Tool capabilities are handled by the setRequestHandler above
const transport = new StdioServerTransport();
server.connect(transport);