Before you begin, make sure you’ve completed the Prerequisites to set up your environment.
Overview
You’ll learn how to:- Build a complete MCP server with tool discovery and execution
- Mount the server on an ASGI server
- Connect an agent to use the MCP server
Building the MCP Server
1
Install dependencies
Install the required packages for a basic MCP server:
2
Import required modules
Import the necessary modules for your MCP server:
3
Create the MCP server instance
Initialize your MCP server with a unique name:This creates the core MCP server that will handle tool discovery and execution.
4
Define your tools
Use the This function returns a list of tools that agents can discover and call. The
@app.list_tools() decorator to define the tools your server exposes. Each tool specifies its name, description, and input schema:inputSchema defines the JSON schema for tool parameters.5
Implement tool logic
Implement the This handler receives the tool name and arguments, executes the appropriate logic, and returns the result as a list of
@app.call_tool() handler to execute your tools:TextContent objects.6
Set up the ASGI server
Create a StreamableHTTPSessionManager and an ASGI handler to serve your MCP server:This handler processes incoming MCP protocol requests and routes them to your server.
7
Mount the server and start it
Create a Starlette application with the MCP handler mounted and start the server:The server is mounted at
/mcp and will handle MCP protocol requests. The lifespan context manager ensures the session manager is properly initialized and cleaned up.8
Run the server
Save your code to a file (e.g., You can also specify custom options:When the server starts successfully, you’ll see output like:Your MCP server is now running and ready to accept tool calls at
main.py) and run it:http://0.0.0.0:5003/mcp.Complete Example
Here’s the complete example of an unmonetized MCP server:Connecting an Agent to Your MCP Server
Now that your MCP server is running, you can connect an agent to use its tools. Here’s how to create a simple LangChain agent that connects to your unmonetized MCP server:1
Install LangChain and PayLink
Install the required packages:
2
Create the agent
Use PayLink’s LangChain integration to connect to your MCP server:The
PayLinkTools class connects to your MCP server at the specified URL and automatically discovers available tools. These tools are then made available to your LangChain agent.3
Use the agent
You can now use the agent to interact with your MCP server tools:The agent will automatically use the
add tool from your MCP server to perform the calculation.Complete Agent Example
Here’s a complete example of a LangChain agent using your unmonetized MCP server:Full Working Example
For a complete, working example of an unmonetized MCP server and agent, see the basic-mcp-agent repository on GitHub.What you achieved
- You built a complete MCP server with tool discovery and execution
- You mounted the server on an ASGI server using Starlette and uvicorn
- You connected a LangChain agent to your MCP server using PayLinkTools
- Your agent can now call tools from your MCP server