Skip to main content

MCP Callback Protocol extension for enabling asynchronous tool callbacks and streaming updates from MCP clients

Project description

MCPC - Model Context Protocol Callback

PyPI version License: MIT Python Versions

MCPC is an extension to MCP (Model-Context-Protocol) that solves a critical limitation in LLM tool interactions: enabling continued conversations while running tools background tasks. It facilitates asynchronous two-way communication between LLMs and tools through the already existing MCP transport - no additional transport layer needed, while maintaining full backward compatibility.

Conceptual demo of MCPC's continuous conversation flow between LLM and tools (UI not included)

What is MCPC?

MCPC is an extension to the MCP protocol, not a replacement. It builds upon the existing MCP infrastructure to add real-time two-way communication capabilities while maintaining full compatibility with standard MCP implementations.

MCPC solves a critical limitation in LLM tool interactions: enabling continuous two-way communication while running background tasks:

  • Bidirectional communication between LLMs and tools using the same MCP transport
  • Continuous conversation with LLMs during tool execution
  • Real-time updates from background processes
  • Asynchronous notifications when operations complete
  • Support for indefinitely running tasks with streaming updates

Compatibility Matrix

Feature Status Notes
STDIO Transport ✅ Implemented Full support for standard input/output transport
SSE Transport ⚠️ Limited Support Standard MCP operations only, MCPC features pending
MCPC Client → Standard MCP Server ✅ Implemented Full backward compatibility
Standard MCP Client → MCPC Server ✅ Implemented Automatic fallback to synchronous results

Quick Installation

# With UV (recommended)
uv add mcpc

# With pip
pip install mcpc

Basic Client Usage

# Initialize the MCPC handler
mcpc_handler = MCPCHandler("my-provider")

# Define your event listener function
async def my_mcpc_listener(mcpc_message: MCPCMessage) -> None:
    print(f"Received MCPC message: {mcpc_message}")
    # Handle the message based on status
    if mcpc_message.type == "task" and mcpc_message.event == "complete":
        print(f"Task {mcpc_message.task_id} completed with result: {mcpc_message.result}")

# Add your event listener for MCPC Message
mcpc_handler.add_event_listener(my_mcpc_listener)

# Wrap the transport with MCPC event listeners
wrapped_transport = await mcpc_handler.wrap_streams(*transport)

# Create a ClientSession with the wrapped transport
session = await ClientSession(*wrapped_transport)

# Initialize MCPC features by checking for MCPC support
mcpc_supported = await mcpc_handler.init_mcpc(session)
if mcpc_supported:
    print(f"MCPC protocol v{mcpc_handler.protocol_version} supported")

Basic Server Usage

# Initialize MCPC helper
server = Server("my-provider")
mcpc = MCPCHelper("my-provider", transport_type="stdio")

@server.call_tool()
async def call_tool(name: str, arguments: Dict[str, Any]) -> List[TextContent]:
    metadata = arguments.pop("_metadata", {})
    session_id = metadata.get("mcpc_session_id", "default")
    task_id = metadata.get("mcpc_task_id", str(uuid.uuid4()))

    # Handle protocol information request
    if name == "_is_mcpc_enabled":
        client_info = arguments.get("mcpc_info")
        return mcpc.handle_protocol_info_request(client_info)

    if name == "process_data":
        async def process_data_task():
            await mcpc.send(mcpc.create_task_event(
                event="update",
                tool_name="process_data",
                session_id=session_id,
                task_id=task_id,
                result="Processing data..."
            ))
            await asyncio.sleep(3) # Simulate processing time
            await mcpc.send(mcpc.create_task_event(
                event="complete",
                tool_name="process_data",
                session_id=session_id,
                task_id=task_id,
                result={
                    YOUR_DATA_OBJECT
                }
            ))
        # Start a background task
        collected_messages = mcpc.start_task(task_id, process_data_task)

    # For standard MCP clients, return collected complete/failed messages
    if collected_messages:
        return mcpc.messages_to_text_content(collected_messages)

    # For MCPC clients, return immediate acknowledgment
    response = mcpc.create_task_event(
        event="created",
        tool_name="process_data",
        session_id=session_id,
        task_id=task_id,
        result=f"Started processing data_id={data_id}. Updates will stream in real-time."
    )
    return mcpc.messages_to_text_content([response])

Documentation

For detailed documentation, please see:

License

MIT

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

mcpc-0.2.4.tar.gz (116.4 kB view details)

Uploaded Source

Built Distribution

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

mcpc-0.2.4-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file mcpc-0.2.4.tar.gz.

File metadata

  • Download URL: mcpc-0.2.4.tar.gz
  • Upload date:
  • Size: 116.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpc-0.2.4.tar.gz
Algorithm Hash digest
SHA256 f0ed93d76111f29d19a9a4b00002d5285dab1aec13acf4b4931ae640cfb67ef7
MD5 e5704292a6c7ee5b0706f9bb00ed5516
BLAKE2b-256 ff7417edcd37fce3c867415efc04d6c8b7d9553c84663bc90adaca9262b74ab6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpc-0.2.4.tar.gz:

Publisher: python-publish.yml on OlaHulleberg/mcpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcpc-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: mcpc-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mcpc-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 917f653310475fa1ec1ba9a95b7b63d8939cbb8903df90808d5af9ab2d671604
MD5 dbc9864f8b0cc248eda0aede92d6df9d
BLAKE2b-256 75c75e2a2d7bae769886c9e9f8032c6bd2c194ee42621ce1d51133b002fb8549

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpc-0.2.4-py3-none-any.whl:

Publisher: python-publish.yml on OlaHulleberg/mcpc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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