Skip to content

helpers

Class info

🛈 DocStrings

Helper functions for MCP server client operations.

This module contains stateless utility functions that support MCP tool conversion and content handling for PydanticAI integration.

extract_text_content

extract_text_content(mcp_content: list[Any]) -> str

Extract text content from MCP content blocks.

Parameters:

Name Type Description Default
mcp_content list[Any]

List of MCP content blocks

required

Returns:

Type Description
str

First available text content or fallback string

Source code in src/llmling_agent/mcp_server/helpers.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def extract_text_content(mcp_content: list[Any]) -> str:
    """Extract text content from MCP content blocks.

    Args:
        mcp_content: List of MCP content blocks

    Returns:
        First available text content or fallback string
    """
    from mcp.types import TextContent

    for block in mcp_content:
        match block:
            case TextContent(text=text):
                return text

    # Fallback: stringify the content
    return str(mcp_content[0]) if mcp_content else "Tool executed successfully"

extract_tool_call_args

extract_tool_call_args(messages: list[Any], tool_call_id: str) -> dict[str, Any]

Extract tool call arguments from message history.

Parameters:

Name Type Description Default
messages list[Any]

List of messages to search through

required
tool_call_id str

ID of the tool call to find

required

Returns:

Type Description
dict[str, Any]

Dictionary of tool call arguments

Source code in src/llmling_agent/mcp_server/helpers.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def extract_tool_call_args(messages: list[Any], tool_call_id: str) -> dict[str, Any]:
    """Extract tool call arguments from message history.

    Args:
        messages: List of messages to search through
        tool_call_id: ID of the tool call to find

    Returns:
        Dictionary of tool call arguments
    """
    for message in messages:
        if hasattr(message, "parts"):
            for part in message.parts:
                if (
                    hasattr(part, "tool_call_id")
                    and part.tool_call_id == tool_call_id
                    and hasattr(part, "args_as_dict")
                ):
                    return part.args_as_dict()
        elif hasattr(message, "content"):
            # Handle different message structures
            for content in message.content:
                if (
                    hasattr(content, "tool_call_id")
                    and content.tool_call_id == tool_call_id
                    and hasattr(content, "args")
                ):
                    return content.args
    return {}

mcp_tool_to_fn_schema

mcp_tool_to_fn_schema(tool: Tool) -> dict[str, Any]

Convert MCP tool to OpenAI function schema format.

Source code in src/llmling_agent/mcp_server/helpers.py
73
74
75
76
77
78
79
def mcp_tool_to_fn_schema(tool: MCPTool) -> dict[str, Any]:
    """Convert MCP tool to OpenAI function schema format."""
    return {
        "name": tool.name,
        "description": tool.description or "",
        "parameters": tool.inputSchema or {"type": "object", "properties": {}},
    }