Skip to main content

mcp_kit.targets.mcp

MCP target implementation for connecting to MCP servers (hosted or with a spec).

McpTarget

class McpTarget(Target)

Target implementation for connecting to MCP servers (hosted or with a spec).

This target can connect to remote MCP servers or use predefined tools. It supports HTTP connections with optional headers and authentication.

__init__

def __init__(name: str,
url: str | None = None,
headers: dict[str, str] | None = None,
tools: list[Tool] | None = None) -> None

Initialize the MCP target.

Arguments:

  • name: Name of the target
  • url: Optional URL of the remote MCP server
  • headers: Optional HTTP headers for server requests
  • tools: Optional predefined tools to use instead of remote server tools

name

@property
def name() -> str

Get the target name.

Returns:

The target name

from_config

@classmethod
def from_config(cls, config: DictConfig) -> Self

Create McpTarget from configuration.

Arguments:

  • config: Target configuration from OmegaConf

Returns:

McpTarget instance

initialize

async def initialize() -> None

Initialize the target by connecting to the MCP server if URL is provided.

Sets up the HTTP connection to the remote MCP server using the configured URL and headers.

list_tools

async def list_tools() -> list[Tool]

List all available tools.

Returns predefined tools if available, otherwise queries the remote MCP server.

Raises:

  • ValueError: If no tools are available and MCP is not initialized

Returns:

List of available tools

call_tool

async def call_tool(name: str,
arguments: dict[str, Any] | None = None) -> list[Content]

Call a tool on the remote MCP server.

Arguments:

  • name: Name of the tool to call
  • arguments: Arguments to pass to the tool

Raises:

  • ValueError: If MCP client is not initialized

Returns:

List of content responses from the tool

close

async def close() -> None

Close the connection to the MCP server.

Cleans up the HTTP connection and releases resources.