Skip to content

vllm_mlx.mcp.tools

Tool schema conversion utilities for MCP <-> OpenAI formats.

View the complete module source at #L1-L174.

API details

Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.

vllm_mlx.mcp.tools

Tool schema conversion utilities for MCP <-> OpenAI formats.

vllm_mlx.mcp.tools.mcp_tool_to_openai

mcp_tool_to_openai(tool: MCPTool) -> Dict[str, Any]

Convert MCP tool schema to OpenAI function calling format.

Parameters:

  • tool (MCPTool) –

    MCPTool instance

Returns:

  • Dict[str, Any]

    OpenAI-compatible tool definition

Source code in vllm_mlx/mcp/tools.py
def mcp_tool_to_openai(tool: MCPTool) -> Dict[str, Any]:
    """
    Convert MCP tool schema to OpenAI function calling format.

    Args:
        tool: MCPTool instance

    Returns:
        OpenAI-compatible tool definition
    """
    return {
        "type": "function",
        "function": {
            "name": tool.full_name,
            "description": tool.description,
            "parameters": tool.input_schema
            or {
                "type": "object",
                "properties": {},
            },
        },
    }

vllm_mlx.mcp.tools.mcp_tools_to_openai

mcp_tools_to_openai(tools: List[MCPTool]) -> List[Dict[str, Any]]

Convert list of MCP tools to OpenAI format.

Parameters:

  • tools (List[MCPTool]) –

    List of MCPTool instances

Returns:

  • List[Dict[str, Any]]

    List of OpenAI-compatible tool definitions

Source code in vllm_mlx/mcp/tools.py
def mcp_tools_to_openai(tools: List[MCPTool]) -> List[Dict[str, Any]]:
    """
    Convert list of MCP tools to OpenAI format.

    Args:
        tools: List of MCPTool instances

    Returns:
        List of OpenAI-compatible tool definitions
    """
    return [mcp_tool_to_openai(tool) for tool in tools]

vllm_mlx.mcp.tools.openai_call_to_mcp

openai_call_to_mcp(tool_call: Dict[str, Any]) -> Tuple[str, str, Dict[str, Any]]

Parse OpenAI tool call back to MCP format.

Parameters:

  • tool_call (Dict[str, Any]) –

    OpenAI tool call from model response

Returns:

  • Tuple[str, str, Dict[str, Any]]

    Tuple of (server_name, tool_name, arguments)

Raises:

  • ValueError

    If tool call format is invalid

Source code in vllm_mlx/mcp/tools.py
def openai_call_to_mcp(tool_call: Dict[str, Any]) -> Tuple[str, str, Dict[str, Any]]:
    """
    Parse OpenAI tool call back to MCP format.

    Args:
        tool_call: OpenAI tool call from model response

    Returns:
        Tuple of (server_name, tool_name, arguments)

    Raises:
        ValueError: If tool call format is invalid
    """
    # Extract function info
    function = tool_call.get("function", {})
    full_name = function.get("name", "")
    arguments_str = function.get("arguments", "{}")

    # Parse arguments
    if isinstance(arguments_str, str):
        try:
            arguments = json.loads(arguments_str)
        except json.JSONDecodeError:
            arguments = {}
    else:
        arguments = arguments_str or {}

    # Split namespaced name (server__tool)
    if "__" in full_name:
        server_name, tool_name = full_name.split("__", 1)
    else:
        # No namespace, use as-is (will need server lookup)
        server_name = ""
        tool_name = full_name

    return server_name, tool_name, arguments

vllm_mlx.mcp.tools.format_tool_result

format_tool_result(result: MCPToolResult, tool_call_id: str) -> Dict[str, Any]

Format tool result for inclusion in conversation messages.

Parameters:

  • result (MCPToolResult) –

    MCPToolResult from tool execution

  • tool_call_id (str) –

    ID of the tool call this is responding to

Returns:

  • Dict[str, Any]

    OpenAI-compatible tool result message

Source code in vllm_mlx/mcp/tools.py
def format_tool_result(result: MCPToolResult, tool_call_id: str) -> Dict[str, Any]:
    """
    Format tool result for inclusion in conversation messages.

    Args:
        result: MCPToolResult from tool execution
        tool_call_id: ID of the tool call this is responding to

    Returns:
        OpenAI-compatible tool result message
    """
    return result.to_message(tool_call_id)

vllm_mlx.mcp.tools.format_tool_results

format_tool_results(results: List[Tuple[MCPToolResult, str]]) -> List[Dict[str, Any]]

Format multiple tool results as messages.

Parameters:

  • results (List[Tuple[MCPToolResult, str]]) –

    List of (MCPToolResult, tool_call_id) tuples

Returns:

  • List[Dict[str, Any]]

    List of OpenAI-compatible tool result messages

Source code in vllm_mlx/mcp/tools.py
def format_tool_results(
    results: List[Tuple[MCPToolResult, str]],
) -> List[Dict[str, Any]]:
    """
    Format multiple tool results as messages.

    Args:
        results: List of (MCPToolResult, tool_call_id) tuples

    Returns:
        List of OpenAI-compatible tool result messages
    """
    return [format_tool_result(result, call_id) for result, call_id in results]

vllm_mlx.mcp.tools.merge_tools

merge_tools(mcp_tools: List[MCPTool], user_tools: Optional[List[Dict[str, Any]]] = None) -> List[Dict[str, Any]]

Merge MCP tools with user-provided tools.

User tools take precedence if there are name conflicts.

Parameters:

  • mcp_tools (List[MCPTool]) –

    Tools discovered from MCP servers

  • user_tools (Optional[List[Dict[str, Any]]], default: None ) –

    User-provided tools in OpenAI format

Returns:

  • List[Dict[str, Any]]

    Combined list of tools in OpenAI format

Source code in vllm_mlx/mcp/tools.py
def merge_tools(
    mcp_tools: List[MCPTool],
    user_tools: Optional[List[Dict[str, Any]]] = None,
) -> List[Dict[str, Any]]:
    """
    Merge MCP tools with user-provided tools.

    User tools take precedence if there are name conflicts.

    Args:
        mcp_tools: Tools discovered from MCP servers
        user_tools: User-provided tools in OpenAI format

    Returns:
        Combined list of tools in OpenAI format
    """
    # Convert MCP tools to OpenAI format
    all_tools = {tool.full_name: mcp_tool_to_openai(tool) for tool in mcp_tools}

    # Add/override with user tools
    if user_tools:
        for tool in user_tools:
            func = tool.get("function", {})
            name = func.get("name", "")
            if name:
                all_tools[name] = tool

    return list(all_tools.values())

vllm_mlx.mcp.tools.extract_tool_calls

extract_tool_calls(response: Dict[str, Any]) -> List[Dict[str, Any]]

Extract tool calls from model response.

Parameters:

  • response (Dict[str, Any]) –

    OpenAI-format model response

Returns:

  • List[Dict[str, Any]]

    List of tool calls

Source code in vllm_mlx/mcp/tools.py
def extract_tool_calls(response: Dict[str, Any]) -> List[Dict[str, Any]]:
    """
    Extract tool calls from model response.

    Args:
        response: OpenAI-format model response

    Returns:
        List of tool calls
    """
    choices = response.get("choices", [])
    if not choices:
        return []

    message = choices[0].get("message", {})
    return message.get("tool_calls", [])

vllm_mlx.mcp.tools.has_tool_calls

has_tool_calls(response: Dict[str, Any]) -> bool

Check if response contains tool calls.

Parameters:

  • response (Dict[str, Any]) –

    OpenAI-format model response

Returns:

  • bool

    True if response contains tool calls

Source code in vllm_mlx/mcp/tools.py
def has_tool_calls(response: Dict[str, Any]) -> bool:
    """
    Check if response contains tool calls.

    Args:
        response: OpenAI-format model response

    Returns:
        True if response contains tool calls
    """
    return len(extract_tool_calls(response)) > 0

Complete contract reference

Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.

vllm_mlx.mcp.tools.mcp_tool_to_openai · function
vllm_mlx.mcp.tools.mcp_tool_to_openai(tool: MCPTool) -> Dict[str, Any]

Convert MCP tool schema to OpenAI function calling format.

Parameters

Name Type Required Default Description
tool MCPTool yes none MCPTool instance

Returns

  • Type: Dict[str, Any]
  • Direct return expressions: {'type': 'function', 'function': {'name': tool.full_name, 'description': tool.description, 'parameters': tool.input_sch…

Exceptions and behavior

Function mcp_tool_to_openai returns {'type': 'function', 'function': {'name': tool.full_name, 'description': tool.description, 'parameters': tool.input_sch…. No direct raise statement appears in this definition.

View source #L12-L33.

vllm_mlx.mcp.tools.mcp_tools_to_openai · function
vllm_mlx.mcp.tools.mcp_tools_to_openai(tools: List[MCPTool]) -> List[Dict[str, Any]]

Convert list of MCP tools to OpenAI format.

Parameters

Name Type Required Default Description
tools List[MCPTool] yes none List of MCPTool instances

Returns

  • Type: List[Dict[str, Any]]
  • Direct return expressions: [mcp_tool_to_openai(tool) for tool in tools]

Exceptions and behavior

Function mcp_tools_to_openai calls mcp_tool_to_openai; returns [mcp_tool_to_openai(tool) for tool in tools]. No direct raise statement appears in this definition.

View source #L36-L46.

vllm_mlx.mcp.tools.openai_call_to_mcp · function
vllm_mlx.mcp.tools.openai_call_to_mcp(tool_call: Dict[str, Any]) -> Tuple[str, str, Dict[str, Any]]

Parse OpenAI tool call back to MCP format.

Parameters

Name Type Required Default Description
tool_call Dict[str, Any] yes none OpenAI tool call from model response

Returns

  • Type: Tuple[str, str, Dict[str, Any]]
  • Direct return expressions: (server_name, tool_name, arguments)

Exceptions and behavior

Function openai_call_to_mcp calls tool_call.get, function.get, isinstance, json.loads; returns (server_name, tool_name, arguments). No direct raise statement appears in this definition.

View source #L49-L84.

vllm_mlx.mcp.tools.format_tool_result · function
vllm_mlx.mcp.tools.format_tool_result(result: MCPToolResult, tool_call_id: str) -> Dict[str, Any]

Format tool result for inclusion in conversation messages.

Parameters

Name Type Required Default Description
result MCPToolResult yes none MCPToolResult from tool execution
tool_call_id str yes none ID of the tool call this is responding to

Returns

  • Type: Dict[str, Any]
  • Direct return expressions: result.to_message(tool_call_id)

Exceptions and behavior

Function format_tool_result calls result.to_message; returns result.to_message(tool_call_id). No direct raise statement appears in this definition.

View source #L87-L98.

vllm_mlx.mcp.tools.format_tool_results · function
vllm_mlx.mcp.tools.format_tool_results(results: List[Tuple[MCPToolResult, str]]) -> List[Dict[str, Any]]

Format multiple tool results as messages.

Parameters

Name Type Required Default Description
results List[Tuple[MCPToolResult, str]] yes none List of (MCPToolResult, tool_call_id) tuples

Returns

  • Type: List[Dict[str, Any]]
  • Direct return expressions: [format_tool_result(result, call_id) for result, call_id in results]

Exceptions and behavior

Function format_tool_results calls format_tool_result; returns [format_tool_result(result, call_id) for result, call_id in results]. No direct raise statement appears in this definition.

View source #L101-L113.

vllm_mlx.mcp.tools.merge_tools · function
vllm_mlx.mcp.tools.merge_tools(mcp_tools: List[MCPTool], user_tools: Optional[List[Dict[str, Any]]] = None) -> List[Dict[str, Any]]

Merge MCP tools with user-provided tools.

Parameters

Name Type Required Default Description
mcp_tools List[MCPTool] yes none Tools discovered from MCP servers
user_tools Optional[List[Dict[str, Any]]] no None User-provided tools in OpenAI format

Returns

  • Type: List[Dict[str, Any]]
  • Direct return expressions: list(all_tools.values())

Exceptions and behavior

Function merge_tools calls mcp_tool_to_openai, tool.get, func.get, list; returns list(all_tools.values()). No direct raise statement appears in this definition.

View source #L116-L143.

vllm_mlx.mcp.tools.extract_tool_calls · function
vllm_mlx.mcp.tools.extract_tool_calls(response: Dict[str, Any]) -> List[Dict[str, Any]]

Extract tool calls from model response.

Parameters

Name Type Required Default Description
response Dict[str, Any] yes none OpenAI-format model response

Returns

  • Type: List[Dict[str, Any]]
  • Direct return expressions: []; message.get('tool_calls', [])

Exceptions and behavior

Function extract_tool_calls calls response.get, choices[0].get, message.get; has 2 explicit return paths. No direct raise statement appears in this definition.

View source #L146-L161.

vllm_mlx.mcp.tools.has_tool_calls · function
vllm_mlx.mcp.tools.has_tool_calls(response: Dict[str, Any]) -> bool

Check if response contains tool calls.

Parameters

Name Type Required Default Description
response Dict[str, Any] yes none OpenAI-format model response

Returns

  • Type: bool
  • Direct return expressions: len(extract_tool_calls(response)) > 0

Exceptions and behavior

Function has_tool_calls calls len, extract_tool_calls; returns len(extract_tool_calls(response)) > 0. No direct raise statement appears in this definition.

View source #L164-L174.

Complete symbol map

This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.

Symbol Kind Signature and inputs What it does Source
mcp_tool_to_openai function mcp_tool_to_openai(tool: MCPTool) -> Dict[str, Any] Convert MCP tool schema to OpenAI function calling format. #L12-L33
mcp_tools_to_openai function mcp_tools_to_openai(tools: List[MCPTool]) -> List[Dict[str, Any]] Convert list of MCP tools to OpenAI format. #L36-L46
openai_call_to_mcp function openai_call_to_mcp(tool_call: Dict[str, Any]) -> Tuple[str, str, Dict[str, Any]] Parse OpenAI tool call back to MCP format. #L49-L84
format_tool_result function format_tool_result(result: MCPToolResult, tool_call_id: str) -> Dict[str, Any] Format tool result for inclusion in conversation messages. #L87-L98
format_tool_results function format_tool_results(results: List[Tuple[MCPToolResult, str]]) -> List[Dict[str, Any]] Format multiple tool results as messages. #L101-L113
merge_tools function merge_tools(mcp_tools: List[MCPTool], user_tools: Optional[List[Dict[str, Any]]] = None) -> List[Dict[str, Any]] Merge MCP tools with user-provided tools. #L116-L143
extract_tool_calls function extract_tool_calls(response: Dict[str, Any]) -> List[Dict[str, Any]] Extract tool calls from model response. #L146-L161
has_tool_calls function has_tool_calls(response: Dict[str, Any]) -> bool Check if response contains tool calls. #L164-L174