MCP (Model Context Protocol) server library for nanoHUB tools
Project description
nanohub-mcp
A Python library for creating Model Context Protocol (MCP) servers that integrate with nanoHUB/HubZero tools infrastructure.
Installation
pip install nanohub-mcp
Quick Start
Using Decorators
from nanohubmcp import MCPServer
server = MCPServer("my-tool")
@server.tool()
def add(a: float, b: float) -> float:
"""Add two numbers together."""
return a + b
@server.tool()
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
@server.resource("config://settings")
def get_settings():
"""Get application settings."""
return {"theme": "dark", "precision": 2}
server.run(port=8000)
Using Folder Structure
Create a project structure:
my-tool/
├── mcp.json
├── tools/
│ ├── calculator.py
│ └── simulator.py
├── resources/
│ └── config.py
└── prompts/
└── templates.py
mcp.json:
{
"name": "my-tool",
"version": "1.0.0",
"tools_dir": "./tools",
"resources_dir": "./resources",
"prompts_dir": "./prompts"
}
tools/calculator.py:
from nanohubmcp import tool
@tool()
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + b
@tool()
def subtract(a: float, b: float) -> float:
"""Subtract b from a."""
return a - b
Run the server:
from nanohubmcp import MCPServer
server = MCPServer.from_config("mcp.json")
server.run()
Using CLI
# Initialize a new project
nanohub-mcp init --name my-tool
# Run the server
cd my-tool
nanohub-mcp run --port 8000
API Reference
MCPServer
The main server class.
from nanohubmcp import MCPServer
# Basic initialization
server = MCPServer("my-server", version="1.0.0")
# With auto-discovery directories
server = MCPServer(
"my-server",
tools_dir="./tools",
resources_dir="./resources",
prompts_dir="./prompts"
)
# From config file
server = MCPServer.from_config("mcp.json")
# Run the server
server.run(host="0.0.0.0", port=8000)
@tool Decorator
Register a function as an MCP tool.
from nanohubmcp import tool
@tool()
def my_function(arg1: str, arg2: int = 10) -> str:
"""Tool description from docstring."""
return f"Result: {arg1}, {arg2}"
# With explicit configuration
@tool(
name="custom_name",
description="Custom description",
input_schema={
"type": "object",
"properties": {
"arg1": {"type": "string", "description": "First argument"}
},
"required": ["arg1"]
}
)
def another_function(arg1: str):
return arg1
@resource Decorator
Register a function as an MCP resource.
from nanohubmcp import resource
@resource("file:///data/config.json")
def read_config():
"""Read configuration file."""
return {"setting": "value"}
@resource("config://app/settings", mime_type="application/json")
def get_settings():
return {"theme": "dark"}
@prompt Decorator
Register a function as an MCP prompt template.
from nanohubmcp import prompt
@prompt()
def explain(topic: str):
"""Generate an explanation prompt."""
return [
{"role": "user", "content": {"type": "text", "text": f"Explain {topic} in simple terms."}}
]
MCP Protocol
This library implements the Model Context Protocol (MCP) using HTTP + Server-Sent Events (SSE) transport:
- GET /sse - SSE endpoint for receiving responses
- POST / - Send JSON-RPC requests
- GET / - Server info
Supported Methods
initialize- Initialize the connectionping- Health checktools/list- List available toolstools/call- Call a toolresources/list- List available resourcesresources/read- Read a resourceprompts/list- List available promptsprompts/get- Get a prompt
Integration with nanoHUB
This library is designed to work with nanoHUB's tool infrastructure. When deployed as a nanoHUB tool, the MCP server is automatically proxied through the HubZero API:
GET /api/mcp/{toolname}/sse - Connect to SSE stream
POST /api/mcp/{toolname} - Send messages
License
MIT License - see LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nanohub_mcp-0.1.1.tar.gz.
File metadata
- Download URL: nanohub_mcp-0.1.1.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88e97b54b732f8fa2302e5fa8abdd4f17eba00e04ddfbbc97d2b35379d0bb83d
|
|
| MD5 |
36b12dfe523b65f1463bfb7534d0117b
|
|
| BLAKE2b-256 |
b5d18bc0df0b6ee5ef96acb8a9b552e62a6aa268c2a76be37716cdde53b9a336
|
File details
Details for the file nanohub_mcp-0.1.1-py3-none-any.whl.
File metadata
- Download URL: nanohub_mcp-0.1.1-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b06daf5d53e6854710c3f33046c7a137b27daf2adf4a3de72833326787128d5a
|
|
| MD5 |
745c8c63fa1b78eac5871b4eaa743dd7
|
|
| BLAKE2b-256 |
7b00e8d8e7e11cd2a116bf97fe0621dd15ba122a43dc23cc648dd6fe8c4a1904
|