Skip to main content

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 connection
  • ping - Health check
  • tools/list - List available tools
  • tools/call - Call a tool
  • resources/list - List available resources
  • resources/read - Read a resource
  • prompts/list - List available prompts
  • prompts/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

nanohub_mcp-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nanohub_mcp-0.1.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file nanohub_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: nanohub_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for nanohub_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 642d5b1420d55ae54460ddbf553eec3d8c03d03baad7060c266ea846889fa84c
MD5 8c709ffb1340368bf51fab9f9c9aba4a
BLAKE2b-256 aaebe28cc8381dd5ef5d424e63dfad96990eb5668258069df9a77eb4a9ccdabf

See more details on using hashes here.

File details

Details for the file nanohub_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nanohub_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for nanohub_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd5d42dd1fcf2311eecc41c5dd4cceb00ab576beed31a7472dad5d14f409d96
MD5 2a1e79e7c7b7ea0cbc4bd155744d030d
BLAKE2b-256 5c4e0a694ddb7fc7391f7af87172a5f92e599f4a716f9b3d3081656bd9df3b61

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page