adachsoft / mcp-tool
AI SPI tool exposing configured Model Context Protocol (MCP) servers and their tools to AI agents.
Requires
- php: >=8.3
- adachsoft/ai-tool-call: ^1.0 || ^2.0
- adachsoft/mcp-contracts: ^0.1
- adachsoft/oauth-client-lib: ^0.1
Requires (Dev)
- adachsoft/mcp-client-lib: ^0.1.0
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/mcp-tool exposes configured Model Context Protocol (MCP) servers through the AI tool interface.
It implements the AI tool SPI of adachsoft/ai-tool-call, so it can be registered as a tool in any agent runtime that consumes the SPI.
Features
- Exposes a single
mcp_tooltool with operations for servers, tools, and calls:list_servers— lists the configured MCP servers without contacting them,list_tools— lists tools exposed by one configured server,call_tool— invokes a specific tool on a configured server.
- Reads server definitions from application configuration (
timeout_seconds,servers,max_result_chars). - Supports per-server and per-operation timeouts, validated against a fixed policy.
- Supports OAuth-protected servers: token retrieval and reactive token refresh on
401responses. - Truncates oversized call output and reports truncation metadata.
Requirements
- PHP
>= 8.3 - Composer 2
- A configured MCP client implementing
AdachSoft\McpContracts\Client\McpClientInterface
Installation
composer require adachsoft/mcp-tool
Integration
Create the tool through AdachSoft\McpTool\Tool\McpToolFactory and register the returned instance in your AI tool dispatcher:
use AdachSoft\McpTool\Config\McpServerConfigParser;
use AdachSoft\McpTool\Tool\McpToolFactory;
$factory = new McpToolFactory($mcpClient, new McpServerConfigParser());
$tool = $factory->create(new ConfigMap([
'timeout_seconds' => 60,
'servers' => [
['id' => 'weather_server', 'url' => 'https://example.com/mcp'],
],
]));
The servers value may be a PHP array or a JSON-encoded string. The tool returned by the factory can then be invoked through the AI tool SPI with operation, server_id, tool_name, and arguments parameters.
Usage
Invoke the tool through the AI tool SPI by wrapping the operation parameters in a ToolCallRequestDto:
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto;
$result = $tool->callTool(new ToolCallRequestDto('mcp_tool', new KeyValueMap([
'operation' => 'list_tools',
'server_id' => 'weather_server',
'timeout' => 30,
])));
Supported operations are list_servers, list_tools, and call_tool. See docs/examples/basic-usage.md for a complete example.
Timeout configuration
The top-level timeout_seconds option sets the default timeout for configured servers. Its default value is 60 seconds when the option is omitted.
A server can override this value with its own timeout_seconds option. An operation can override the effective server timeout with the optional timeout parameter used with list_servers, list_tools, or call_tool.
Timeout values must be integers from 1 to 300 seconds. Values outside this range are rejected before the MCP request is sent.
Example:
{
"timeout_seconds": 60,
"servers": [
{
"id": "weather_server",
"url": "https://example.com/mcp",
"timeout_seconds": 120
}
]
}
The configured or operation-specific timeout is applied to the MCP HTTP request, including session initialization.