haive.core.common.mixins.mcp_mixin¶

MCP (Model Context Protocol) mixin for adding MCP support to configurations.

This module provides a mixin that enhances configuration classes with MCP integration capabilities. It enables automatic discovery and wrapping of MCP tools, resource management, and prompt template integration while maintaining compatibility with existing Haive patterns.

Usage:

from pydantic import BaseModel from haive.core.common.mixins import MCPMixin from haive.mcp.config import MCPConfig, MCPServerConfig

class MyConfig(MCPMixin, BaseModel):

name: str

# Create config with MCP support config = MyConfig(

name=”agent”, mcp_config=MCPConfig(

enabled=True, servers={

“filesystem”: MCPServerConfig(

transport=”stdio”, command=”npx”, args=[“-y”, “@modelcontextprotocol/server-filesystem”]

)

}

)

)

# Initialize MCP (discovers tools, resources, prompts) await config.setup_mcp()

# Access MCP tools (automatically wrapped) tools = config.get_mcp_tools()

# Access MCP resources resources = config.get_mcp_resources()

# Use MCP-enhanced system prompt enhanced_prompt = config.enhance_system_prompt_with_mcp(“Base prompt”)

Classes¶

MCPMixin

Mixin for adding MCP (Model Context Protocol) support to configurations.

MCPPromptTemplate

Model representing an MCP prompt template.

MCPResource

Model representing an MCP resource.

MCPToolWrapper

Wrapper to convert MCP tools to Haive-compatible tools.

Module Contents¶

class haive.core.common.mixins.mcp_mixin.MCPMixin(/, **data)[source]¶

Bases: pydantic.BaseModel

Mixin for adding MCP (Model Context Protocol) support to configurations.

This mixin provides seamless integration with MCP servers, enabling: - Automatic discovery and wrapping of MCP tools - Resource loading and caching from MCP servers - Prompt template management - Enhanced system prompts with MCP information

The mixin is designed to work with ToolRouteMixin for proper tool routing and can be combined with other mixins in the configuration hierarchy.

Parameters:

data (Any)

mcp_config¶

Optional MCP configuration for server connections

mcp_resources¶

List of discovered MCP resources

mcp_prompts¶

Dictionary of MCP prompt templates

auto_discover_mcp_tools¶

Whether to automatically discover MCP tools

inject_mcp_resources¶

Whether to inject resources into context

use_mcp_prompts¶

Whether to use MCP prompts for enhancement

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

async call_mcp_prompt(prompt_name, arguments=None)[source]¶

Call an MCP prompt to get formatted messages.

Parameters:
  • prompt_name (str) – Name of the prompt (can include server prefix)

  • arguments (dict[str, Any] | None) – Arguments to pass to the prompt

Returns:

List of message dictionaries with role and content

Raises:

ValueError – If prompt not found or MCP not initialized

Return type:

list[dict[str, str]]

cleanup_mcp()[source]¶

Clean up MCP resources.

This should be called when the configuration is no longer needed to properly close MCP connections.

Return type:

None

enhance_system_prompt_with_mcp(base_prompt='')[source]¶

Enhance a system prompt with MCP information.

Adds information about available MCP resources and operations to help the LLM understand what capabilities are available.

Parameters:

base_prompt (str) – Base system prompt to enhance

Returns:

Enhanced system prompt including MCP resources and capabilities

Return type:

str

get_mcp_prompts()[source]¶

Get all loaded MCP prompt templates.

Returns:

Dictionary of prompt templates by name

Return type:

dict[str, MCPPromptTemplate]

async get_mcp_resource_content(uri)[source]¶

Fetch content for an MCP resource.

Parameters:

uri (str) – Resource URI

Returns:

Resource content

Raises:

ValueError – If MCP manager not initialized or resource not found

Return type:

Any

get_mcp_resources()[source]¶

Get all loaded MCP resources.

Returns:

List of MCP resources

Return type:

list[MCPResource]

get_mcp_tools()[source]¶

Get all discovered MCP tools.

Returns:

List of MCP tool wrappers

Return type:

list[MCPToolWrapper]

async setup_mcp()[source]¶

Initialize MCP integration.

Sets up the MCP manager, discovers tools, loads resources, and configures prompts based on the MCP configuration.

This method should be called after creating the configuration but before using any MCP features.

Return type:

None

class haive.core.common.mixins.mcp_mixin.MCPPromptTemplate(/, **data)[source]¶

Bases: pydantic.BaseModel

Model representing an MCP prompt template.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class haive.core.common.mixins.mcp_mixin.MCPResource(/, **data)[source]¶

Bases: pydantic.BaseModel

Model representing an MCP resource.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

class haive.core.common.mixins.mcp_mixin.MCPToolWrapper(**kwargs)[source]¶

Bases: langchain_core.tools.BaseTool

Wrapper to convert MCP tools to Haive-compatible tools.

This wrapper allows MCP tools to be used seamlessly within the Haive framework by adapting their interface to match BaseTool expectations.

Initialize the tool.

Parameters:

kwargs (Any)

description: str¶

Used to tell the model how/when/why to use the tool.

You can provide few-shot examples as a part of the description.

name: str¶

The unique name of the tool that clearly communicates its purpose.