Skip to main content

Sema4AI Model Context Protocol (MCP)

Project description

Sema4AI Model Context Protocol (MCP) Library

The Model Context Protocol (MCP) is a protocol that AI agents can use to take action based on the output of a conversation with an LLM. This library provides an API that allows you to implement MCP tools, resources and prompts in your Python code.

It works a bit different from other MCP implementations in that the idea is that you'll just worry about the MCP tool/resource/prompt implementation in Python, but the actual management of the connection and lifecycle of your tool/resource/prompt functions is handled by the Sema4AI Action Server.

This means that you should just focus on the MCP functions, NOT on features provided by the Sema4AI Action Server such as:

  • Running the server
  • Making logging (which is automatically setup using robocorp.log)
  • Creating your python environment (the environment is managed by a package.yaml file that's expected to be in the root of your project)
  • Handling connections
  • Providing a separate REST API to call your MCP functions (the regular REST API provided for actions in the Sema4AI Action Server may also be used to call your MCP functions)

Developing MCP functions:

Using the Sema4ai VSCode Extension, enables you to easily run your MCP functions locally and later on deploy as you'd like -- you can easily deploy your MCP server and agents to the cloud by using Sema4AI -- or just use the Sema4AI Action Server in a private cloud or locally as well.

Installation

pip install sema4ai-mcp

Usage

The package provides three main decorators:

@tool

Use the @tool decorator to define functions that can be used as tools by AI agents:

from sema4ai.mcp import tool

@tool
def assign_ticket(ticket_id: str, user_id: str) -> bool:
    """
    Assign a ticket to a user.

    Args:
        ticket_id: The ID of the ticket to assign.
        user_id: The ID of the user to assign the ticket to.

    Returns:
        True if the ticket was assigned successfully, False otherwise.
    """
    ...
    return True

The @tool decorator can also be used with the following arguments:

  • title: A human-readable title for the tool.
  • read_only_hint: If true, the tool does not modify its environment (default: False).
  • destructive_hint: If true, the tool may perform destructive updates to its environment (default: True).
  • idempotent_hint: If true, calling the tool repeatedly with the same arguments will have no additional effect on the its environment (default: False).
  • open_world_hint: If true, this tool may interact with an "open world" of external entities (default: True). If False, the tool's domain of interaction is closed. For example, the world of a web search tool is open, whereas that of a memory tool is not.

@resource

Use the @resource decorator to define functions that provide resources to the LLM:

from sema4ai.mcp import resource

@resource("tickets://{ticket_id}")
def get_ticket(ticket_id: str) -> dict[str, str]:
    """
    Get ticket information from the database.

    Args:
        ticket_id: The ID of the ticket to get information for.

    Returns:
        A dictionary containing the ticket information.
    """
    return {"id": ticket_id, "summary": "This is a test ticket"}

@prompt

Use the @prompt decorator to define functions that generate prompts.

from sema4ai.mcp import prompt

@prompt
def make_a_summary(text: str) -> str:
    """
    Provide a prompt to the LLM.
    """
    return "Please make a summary of the following text: {text}"

License

This project is licensed under the Apache License 2.0.

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

sema4ai_mcp-0.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

sema4ai_mcp-0.0.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file sema4ai_mcp-0.0.2.tar.gz.

File metadata

  • Download URL: sema4ai_mcp-0.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for sema4ai_mcp-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ea8e1230985474faea7c74b80d02af48aaa4cc8df4c35da95a51cc13ce69b745
MD5 e002bd968b8c67ab6d53b5db0ff58a2d
BLAKE2b-256 2ab81bf218a3680c2a112764fd30460a345b37d7312d096eaad144f699801f6b

See more details on using hashes here.

File details

Details for the file sema4ai_mcp-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: sema4ai_mcp-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for sema4ai_mcp-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a9dbe748bd4651626a3931995c7a934cbdcb8e13bf69ef225e01ee20c659ec1f
MD5 a1e0a640a68f5cdc60fd613f8d750aee
BLAKE2b-256 ebcc2fbbdea0705b92bee1ce6d15a7f2e5f3a29a8541d3e64903d19f73e78596

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