import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { generateDadJoke } from './tools/jokeGenerator.js';
import { generateImage } from './tools/imageGenerator.js';
import { createWebPage, startWebServer } from './tools/webPageCreator.js';
class DadJokeVisualizerServer {
private server: Server;
constructor() {
this.server = new Server(
{
name: 'dad-joke-visualizer',
version: '1.0.0',
}
);
this.setupHandlers();
}
private setupHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: 'generate_dad_joke_with_visualization',
description: 'Generates a Dad Joke with customizable content rating and creates a visualization, then displays both on a web page',
inputSchema: {
type: 'object',
properties: {
topic: {
type: 'string',
description: 'Optional topic for the Dad Joke (e.g., "cats", "programming", "food")',
},
rating: {
type: 'string',
enum: ['PG', 'PG-13', 'R'],
description: 'Content rating: PG (Clean & Family Friendly), PG-13 (Mild Adult Humor), R (Mature Audiences Only)',
default: 'PG-13',
},
},
required: [],
},
},
],
};
});
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (name === 'generate_dad_joke_with_visualization') {
try {
const topic = args?.topic as string | undefined;
const rating = args?.rating as string | undefined || 'PG-13';
// Step 1: Generate Dad Joke with rating
const joke = await generateDadJoke(topic, rating);
// Step 2: Generate Image
const imageUrl = await generateImage(joke, topic, rating);
// Step 3: Create Web Page
const webPageUrl = await createWebPage(joke, imageUrl);
return {
content: [
{
type: 'text',
text: `π **${rating} Dad Joke Generated Successfully!**\n\n**Rating:** ${rating} ${rating === 'PG' ? 'π' : rating === 'PG-13' ? 'π' : 'π'}\n\n**Joke:** ${joke}\n\n**Visualization:** ${imageUrl}\n\n**View Complete Page:** ${webPageUrl}`,
},
],
};
} catch (error) {
return {
content: [
{
type: 'text',
text: `β Error generating Dad Joke: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
throw new Error(`Unknown tool: ${name}`);
});
}
async run() {
// Start the web server first
await startWebServer();
// Then start the MCP server
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('Dad Joke Visualizer MCP Server running on stdio');
}
}
const server = new DadJokeVisualizerServer();
server.run().catch(console.error);