py2mcp

py2mcp: Quick MCP server creation from Python functions.

This package provides a simple, Pythonic way to create Model Context Protocol (MCP) servers from ordinary Python functions. Built on FastMCP, it handles all the protocol complexity while letting you focus on your business logic.

Basic usage:
>>> from py2mcp import mk_mcp_server
>>>
>>> def add(a: int, b: int) -> int:
...     '''Add two numbers'''
...     return a + b
>>>
>>> mcp = mk_mcp_server([add])
>>> # mcp.run()  # Start the server
py2mcp.mk_input_trans(name_func_relationships: Mapping | None = None) Callable[[dict], dict][source]

Create an input transformation function from name->func mappings.

>>> def to_int(x): return int(x)
>>> trans = mk_input_trans({'x': to_int})
>>> trans({'x': '42', 'y': 'hello'})
{'x': 42, 'y': 'hello'}
py2mcp.mk_mcp_from_store(store: MutableMapping[Any, Any], *, name: str = 'item', plural: str = '', server_name: str | None = None) FastMCP[source]

Create an MCP server from a MutableMapping with CRUD operations.

Automatically generates list, get, set, and delete functions for the store.

Parameters:
  • store – A MutableMapping to expose via MCP

  • name – Singular name for items (e.g., ‘project’, ‘user’)

  • plural – Plural form (defaults to name + ‘s’)

  • server_name – Name of the MCP server (defaults to “{name} Store”)

Returns:

A FastMCP server with CRUD operations

Examples

>>> projects = {'p1': {'name': 'Project 1'}, 'p2': {'name': 'Project 2'}}
>>> mcp = mk_mcp_from_store(projects, name='project')
>>> mcp.name
'project Store'
py2mcp.mk_mcp_server(funcs: Callable | Iterable[Callable], *, name: str = 'py2mcp Server', input_trans: Callable[[dict], dict] | None = None) FastMCP[source]

Create an MCP server from Python functions.

This is the main entry point for py2mcp. Pass one or more functions, and get back a FastMCP server ready to run.

Parameters:
  • funcs – A function or iterable of functions to expose as MCP tools

  • name – Name of the MCP server

  • input_trans – Optional function to transform input kwargs before calling tools

Returns:

A FastMCP server instance ready to run

Examples

>>> def add(a: int, b: int) -> int:
...     '''Add two numbers'''
...     return a + b
>>> mcp = mk_mcp_server(add)
>>> mcp.name
'py2mcp Server'
>>> def greet(name: str) -> str:
...     return f"Hello, {name}!"
>>> mcp = mk_mcp_server([add, greet], name="Math & Greetings")
>>> mcp.name
'Math & Greetings'