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.1.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.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sema4ai_mcp-0.0.1.tar.gz
Algorithm Hash digest
SHA256 56974212bdc3d8274c8175e3f1693fe59e6d2ddf97d9eb9a676f5baf524d2bbf
MD5 fcbcc55ea86dfb9ae14de6f3fe9c82c6
BLAKE2b-256 7b2fa06ade42610b3be5b53ccc5673793fdac0070d6acf242d11c631a1b63cf1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sema4ai_mcp-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64a96f5f8bacaaf50ede374a54a5ddf0ca8ced19446450a41b3c2c97a0e4f418
MD5 d6964dd1f6592a7a73a7f1cd5f6e6631
BLAKE2b-256 a4c6597e7de73a036d9f77f791714c35769d04a2fc876da7a7b185b07cf5c5aa

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