How to beginner · 3 min read

How to add prompts to MCP server

Quick answer
To add prompts to an MCP server, use the official mcp Python SDK by creating a Server instance and passing prompt messages via the stdio_server or SSE transport. Define your prompt as a message in the input stream, and the server will process it accordingly.

PREREQUISITES

  • Python 3.8+
  • pip install mcp
  • Basic knowledge of Python async programming

Setup

Install the official mcp Python SDK and prepare your environment.

  • Run pip install mcp to install the SDK.
  • No API key is required since MCP servers run locally or in your environment.
bash
pip install mcp

Step by step

This example shows how to add prompts to an MCP server using the stdio_server transport. The server reads prompts from standard input and processes them.

python
import asyncio
from mcp.server import Server
from mcp.server.stdio import stdio_server

async def main():
    # Define a simple handler that echoes the prompt
    async def handler(prompt):
        # Here you can add logic to process the prompt
        return f"Received prompt: {prompt}"

    # Create the MCP server with the handler
    server = Server(handler=handler)

    # Run the stdio server which reads prompts from stdin
    await stdio_server(server)

if __name__ == "__main__":
    asyncio.run(main())

Common variations

You can customize prompt handling by modifying the handler function to parse and respond to different prompt formats or integrate with other AI models.

  • Use SSE transport instead of stdio for web-based communication.
  • Integrate with OpenAI or Anthropic clients inside the handler for AI-powered responses.
  • Run the server asynchronously to handle multiple prompts concurrently.
python
import asyncio
from mcp.server import Server
from mcp.server.sse import sse_server

async def handler(prompt):
    # Example: simple keyword check
    if "hello" in prompt.lower():
        return "Hi there! How can I help you?"
    return "Prompt received."

async def main():
    server = Server(handler=handler)
    await sse_server(server, host='localhost', port=8080)

if __name__ == "__main__":
    asyncio.run(main())

Troubleshooting

If the MCP server does not respond to prompts, ensure you are running the server in an environment where standard input/output is accessible (for stdio_server) or that the SSE server port is open and not blocked by firewalls.

Check for exceptions in your handler function and add logging to diagnose prompt processing issues.

Key Takeaways

  • Use the official mcp Python SDK to add prompts to MCP servers.
  • Define a prompt handler function to process and respond to incoming prompts.
  • Choose between stdio_server for CLI or sse_server for web transport.
  • Run the MCP server asynchronously with asyncio.run().
  • Add logging and error handling in the handler to troubleshoot prompt issues.
Verified 2026-04
Verify ↗