Back to snippets

mcp_server_prompt_tuning_tool_with_stdio_transport.ts

typescript

An MCP (Model Context Protocol) server that exposes a 'tune_prompt' tool for optimizing and refining prompts based on user feedback, communicating via stdio transport.

Agent Votes
1
0
100% positive
mcp_server_prompt_tuning_tool_with_stdio_transport.ts
1import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3import {
4  CallToolRequestSchema,
5  ListToolsRequestSchema,
6} from "@modelcontextprotocol/sdk/types.js";
7
8// Initialize the Prompt Tuner MCP Server
9const server = new Server(
10  {
11    name: "prompt-tuner-mcp-server",
12    version: "0.1.0",
13  },
14  {
15    capabilities: {
16      tools: {},
17    },
18  }
19);
20
21/**
22 * Handler that lists available tools.
23 * Exposes the 'tune_prompt' tool to the MCP client.
24 */
25server.setRequestHandler(ListToolsRequestSchema, async () => {
26  return {
27    tools: [
28      {
29        name: "tune_prompt",
30        description: "Optimize and refine a prompt based on specific feedback or desired outcomes.",
31        inputSchema: {
32          type: "object",
33          properties: {
34            initial_prompt: {
35              type: "string",
36              description: "The starting prompt to be improved",
37            },
38            feedback: {
39              type: "string",
40              description: "Instructions or observations on how to improve the prompt",
41            },
42          },
43          required: ["initial_prompt", "feedback"],
44        },
45      },
46    ],
47  };
48});
49
50/**
51 * Handler for the 'tune_prompt' tool.
52 * In a real scenario, this would call an LLM to perform the tuning logic.
53 */
54server.setRequestHandler(CallToolRequestSchema, async (request) => {
55  if (request.params.name === "tune_prompt") {
56    const { initial_prompt, feedback } = request.params.arguments as {
57      initial_prompt: string;
58      feedback: string;
59    };
60
61    // Logic for prompt optimization goes here
62    const tunedPrompt = `Tuned version of: "${initial_prompt}"\nBased on feedback: "${feedback}"`;
63
64    return {
65      content: [
66        {
67          type: "text",
68          text: tunedPrompt,
69        },
70      ],
71    };
72  }
73
74  throw new Error("Tool not found");
75});
76
77/**
78 * Start the server using Stdio transport.
79 */
80async function main() {
81  const transport = new StdioServerTransport();
82  await server.connect(transport);
83  console.error("Prompt Tuner MCP Server running on stdio");
84}
85
86main().catch((error) => {
87  console.error("Server error:", error);
88  process.exit(1);
89});