定制 tourze/mcp-contracts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/mcp-contracts

最新稳定版本:1.0.0

Composer 安装命令:

composer require tourze/mcp-contracts

包简介

MCP契约

README 文档

README

English | 中文

Latest Version Build Status Quality Score Total Downloads

A PHP library providing contracts and interfaces for MCP (Model Context Protocol) tools integration, designed for Symfony applications.

Features

  • Tool Interface: Defines standard interface for MCP tools
  • Attribute Support: Provides AsTool attribute for tool registration
  • Symfony Integration: Built-in support for Symfony's dependency injection
  • Type Safety: Full PHP 8.1+ type declarations
  • Extensible: Easy to extend and customize for specific needs

Installation

composer require tourze/mcp-contracts

Quick Start

1. Implement the Tool Interface

<?php

use Tourze\MCPContracts\ToolInterface;
use Tourze\MCPContracts\Attribute\AsTool;
use OpenAIBundle\VO\FunctionParam;

#[AsTool(
    name: 'weather_tool',
    description: 'Get weather information for a specific location'
)]
class WeatherTool implements ToolInterface
{
    public function getName(): string
    {
        return 'weather_tool';
    }

    public function getDescription(): string
    {
        return 'Get weather information for a specific location';
    }

    public function getParameters(): \Traversable
    {
        yield new FunctionParam('location', 'string', 'City name or coordinates');
        yield new FunctionParam('units', 'string', 'Temperature units (celsius/fahrenheit)', false);
    }

    public function execute(array $parameters = []): string
    {
        $location = $parameters['location'] ?? '';
        $units = $parameters['units'] ?? 'celsius';
        
        // Your weather API integration here
        return "Weather in {$location}: 22°C, sunny";
    }
}

2. Register as Symfony Service

The tool will be automatically registered as a service with the mcp.tool tag thanks to the AutoconfigureTag attribute.

# config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true

    App\Tool\:
        resource: '../src/Tool/'

3. Use in Your Application

<?php

use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
use Tourze\MCPContracts\ToolInterface;

class ToolManager
{
    /** @param iterable<ToolInterface> $tools */
    public function __construct(
        #[AutowireIterator('mcp.tool')] private iterable $tools
    ) {}

    public function getTools(): array
    {
        return iterator_to_array($this->tools);
    }

    public function executeTool(string $name, array $parameters = []): string
    {
        foreach ($this->tools as $tool) {
            if ($tool->getName() === $name) {
                return $tool->execute($parameters);
            }
        }

        throw new \RuntimeException("Tool '{$name}' not found");
    }
}

API Reference

ToolInterface

Main interface for MCP tools.

Methods

  • getName(): string - Returns the tool's unique name
  • getDescription(): string - Returns tool description for LLM context
  • getParameters(): \Traversable<FunctionParam> - Returns parameter definitions
  • execute(array $parameters = []): string - Executes the tool with given parameters

AsTool Attribute

Attribute for tool registration and metadata.

Properties

  • string $name - Tool name
  • string $description - Tool description
  • string $method - Method to call (default: __invoke)

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 64
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-26