Build Your Own MCP Server

A Model Context Protocol (MCP) server is a small web service that exposes tools—typed functions the model can call. Think of it as “bring-your-own tools over a standard protocol”.

Enable Agents to Interact with Your Own Code with FastMCP

FastMCP is a python library that allows fast and simple creation of Model Context Protocol (MCP) servers, a standardized way to connect LLMs to tools and data.

Below is a simple python template to create your own MCP server and connect agents and code. Customize it to create new tools and functions according to your needs.

  1. Install fastmcp library

    pip install fastmcp
  2. Copy, paste and customize this code on a python file.

    import logging
    from fastmcp import FastMCP
    logging.basicConfig(
       level=logging.INFO,
       format='%(asctime)s - %(levelname)s - %(message)s'
    )
    logger = logging.getLogger(__name__)
    mcp = FastMCP(name="Custom_Server")
    def log_request(tool_name: str, **kwargs):
       logger.info(f"[REQUEST RECEIVED] Tool: {tool_name}, Args: {kwargs}")
    def log_response(tool_name: str, result):
       logger.info(f"[RESPONSE SENT] Tool: {tool_name}, Result: {result}")
    @mcp.tool
    def custom_tool_name(a: a_type, b: b_type) -> type:
       log_request("my_custom_tool", a=a, b=b)
       response = custom_function(a,b)
       log_response("custom_tool_name", response)
       return response
    @mcp.resource("resource://config")
    def get_config() -> dict:
       return {"version": "1.1", "author": "FastMCP"}
    #Step 3: Initialize your MCP-Server using HTTP + SSE transport protocol
    #   mcp.run(transport="sse", …) — starts the MCP server using the SSE transport.
    #   host: network interface/hostname to bind.
    #   Use 127.0.0.1 for local-only, 0.0.0.0 for all interfaces, or a FQDN that resolves to your machine.
    #   port: TCP port to listen on (e.g., 8080).
    #   Mount path: the SSE stream is served under /sse by default.
    if __name__ == "__main__":
       # Runs an SSE transport server; by default it mounts at /sse on localhost.
       # You can customize host/port/mount_path via mcp.settings.* if needed.
       mcp.run(transport="sse", host="your-host", port=PORT)