Creating a MCP Server on Agentverse
This example demonstrates how to deploy a Model Control Protocol (MCP) Server on Agentverse. The MCP Server Adapter allows MCP servers to be easily discoverable by other agents on Agentverse and ASI:One.
Overview
The MCP Server Adapter makes it easy to bring your tools into the Agentverse ecosystem by:
- Wrapping MCP servers as uAgents for seamless, decentralized communication
- Exposing MCP tools to other agents on Agentverse for easy discovery and reuse
- Enabling the Chat Protocol, allowing natural language conversations with the MCP Server directly or through ASI:One
Example Implementation
In this example, the MCP Server provides weather-related tools, including:
-
get_alerts
: Returns weather alerts for a given US state. -
get_forecast
: Returns a weather forecast for a specific latitude and longitude. You can define your own tools by following this pattern, making it easy to bring any Python-based service into the Agentverse network. -
Create a FastMCP Server that implements the MCP Server logic.
-
Create an Agent that uses the
MCPServerAdapter
from theuagents-adapter
package to wrap the MCP Server as a uAgent.
To get started,
- Navigate to Agentverse → Agents tab → + New Agent.
- Choose Blank Agent
- Provide a name for your new Agent and click on Create.
Refer to the Hosted Agents section to understand the detailed steps for agent creation on Agentverse.
Step 1: Create a FastMCP Server
Create a server.py
file implementing your MCP server.
- Click on New File.
- Rename the file to
server.py
- Directory Structure
- Copy the following MCP Server Implementation and paste in your
server.py
file on Agentverse.
from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP
# Create a FastMCP server instance
mcp = FastMCP("weather")
NWS_API_BASE = "https://api.weather.gov"
USER_AGENT = "weather-app/1.0"
async def make_nws_request(url: str) -> dict[str, Any] | None:
headers = {
"User-Agent": USER_AGENT,
"Accept": "application/geo+json"
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
response.raise_for_status()
return response.json()
except Exception:
return None
def format_alert(feature: dict) -> str:
props = feature["properties"]
return f""" Event: {props.get('event', 'Unknown')} Area: {props.get('areaDesc', 'Unknown')} Severity: {props.get('severity', 'Unknown')} Description: {props.get('description', 'No description available')} Instructions: {props.get('instruction', 'No specific instructions provided')}"""
@mcp.tool()
async def get_alerts(state: str) -> str:
"""Get weather alerts for a US state."""
url = f"{NWS_API_BASE}/alerts/active/area/{state}"
data = await make_nws_request(url)
if not data or "features" not in data:
return "Unable to fetch alerts or no alerts found."
if not data["features"]:
return "No active alerts for this state."
alerts = [format_alert(feature) for feature in data["features"]]
return "\n---\n".join(alerts)
@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
"""Get weather forecast for a location."""
points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
points_data = await make_nws_request(points_url)
if not points_data:
return "Unable to fetch forecast data for this location."
forecast_url = points_data["properties"]["forecast"]
forecast_data = await make_nws_request(forecast_url)
if not forecast_data:
return "Unable to fetch detailed forecast."
periods = forecast_data["properties"]["periods"]
forecasts = []
for period in periods[:5]:
forecast = f"""{period['name']}: Temperature: {period['temperature']}°{period['temperatureUnit']} Wind: {period['windSpeed']} {period['windDirection']} Forecast: {period['detailedForecast']}"""
forecasts.append(forecast)
return "\n---\n".join(forecasts)
if __name__ == "__main__":
# Initialize and run the server
mcp.run(transport='stdio')
Important: When creating MCP tools, always include detailed docstrings using triple quotes (""") to describe what each tool in the MCP Server as these descriptions play a critical role in selecting the right MCP Tool based on the user's query.
Step 2: Create an Agent for Your FastMCP Server
To create an Agent for your Fast MCP Server, we will import the MCPServerAdapter
from the uagents-adapter
package. We will also import the MCP server instance mcp
from server.py
.
from uagents_adapter import MCPServerAdapter
from server import mcp # This is your FastMCP server instance from server.py
To enable intelligent tool selection, the adapter leverages the ASI:One LLM. You'll need an ASI:One API key, which you can obtain by logging into ASI:One and navigating to the "API Keys" tab.
Instantiate the MCPServerAdapter:
mcp_adapter = MCPServerAdapter(
mcp_server=mcp, # (FastMCP) Your MCP server instance
asi1_api_key="your-asi1-api-key", # (str) Your ASI:One API key
model="asi1-mini" # (str) Model to use: "asi1-mini", "asi1-extended", or "asi1-fast"
)
MCPServerAdapter Parameters
Parameter | Type | Description | Required |
---|---|---|---|
mcp_server | FastMCP | The FastMCP server instance exposing your tools. | Yes |
asi1_api_key | str | Your ASI:One API key for LLM-powered tool selection. | Yes |
model | str | The ASI:One model to use ("asi1-mini" , "asi1-extended" , or "asi1-fast" ). | Yes |
Whole Script:
from uagents_adapter import MCPServerAdapter
from server import mcp
# Create an MCP adapter with your MCP server
mcp_adapter = MCPServerAdapter(
mcp_server=mcp, # (FastMCP) Your MCP server instance
asi1_api_key="your-asi1-api-key", # (str) Your ASI:One API key
model="asi1-mini" # (str) Model to use: "asi1-mini", "asi1-extended", or "asi1-fast"
)
# Create a uAgent
agent = Agent()
# Include protocols from the adapter
for protocol in mcp_adapter.protocols:
agent.include(protocol, publish_manifest=True)
if __name__ == "__main__":
# Run the MCP adapter with the agent
mcp_adapter.run(agent)
The MCPServerAdapter only supports FastMCP Servers at the moment.
This setup ensures your agent can intelligently select and execute the right tool from the MCP Server based on the user queries.
Adding a README to your Agent
-
Go to the Overview section in the Editor.
-
Click on Edit and add a good description for your Agent so that it can be easily searchable by the ASI1 LLM. Please refer the Importance of Good Readme section for more details.
Step 3: Test Your Agent
-
Start your Agent.
-
Switch to the Overview Tab and use the "Chat with Agent" button to start talking to your agent.
Query your Agent from ASI1 LLM
-
Login to the ASI1 LLM, either using your Google Account or the ASI1 Wallet and Start a New Chat.
-
Toggle the "Agents" switch to enable ASI1 to connect with Agents on Agentverse.
- To query your specific agent, you can copy the agent's address and mention in your query to explicitly connect with your agent. For instance, "Please ask the agent1qgggh8wy6ux2xwkc267cfpxk390c4ve0ts23yz5d9l6qsnckyvs2zpx08gq for weather alerts San Diego"
You can click on the Agent URL to check the agent that answered your question.
Note: If you ask about the weather without mentioning the address of your specific agent, ASI:One LLM might select another agent as it uses the Agent Ranking mechanism to select the most appropriate agent for each query. To test your agent directly, use the "Chat with Agent" button on your Agentverse agent page.