Price Per TokenPrice Per Token

Calculator MCP Server

by modelcontextprotocol

0

About

MCP PHP SDK is the official PHP framework for implementing Model Context Protocol servers and clients. Developed in collaboration between the PHP Foundation and Symfony, it provides attribute-based discovery to expose PHP methods as AI-callable tools and resources. Key capabilities include: - Attribute-driven MCP server development using `#[McpTool]` and `#[McpResource]` annotations - STDIO transport for local MCP client integration - Built-in calculator tools supporting addition, subtraction, multiplication, and division operations - JSON configuration resource management - Framework-agnostic architecture following Symfony coding standards and backward compatibility promises - Automatic discovery of MCP capabilities from PHP classes

README

MCP PHP SDK

The official PHP SDK for Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP servers and clients in PHP.

This project represents a collaboration between the PHP Foundation and the Symfony project. It adopts development practices and standards from the Symfony project, including Coding Standards and the Backward Compatibility Promise.

Until the first major release, this SDK is considered experimental, please see the roadmap for planned next steps and features.

Installation

composer require mcp/sdk

Quick Start

This example demonstrates the most common usage pattern - a STDIO server using attribute discovery.

1. Define Your MCP Elements

Create a class with MCP capabilities using attributes:

 $a + $b,
            'subtract' => $a - $b,
            'multiply' => $a * $b,
            'divide' => $b != 0 ? $a / $b : 'Error: Division by zero',
            default => 'Error: Unknown operation'
        };
    }

#[McpResource( uri: 'config://calculator/settings', name: 'calculator_config', mimeType: 'application/json' )] public function getSettings(): array { return ['precision' => 2, 'allow_negative' => true]; } }

2. Create the Server Script

Create your MCP server:

#!/usr/bin/env php
setServerInfo('Calculator Server', '1.0.0')
    ->setDiscovery(__DIR__, ['.'])
    ->build();

$transport = new StdioTransport();

$server->run($transport);

3. Configure Your MCP Client

Add to your client configuration (e.g., Claude Desktop's mcp.json):

{
    "mcpServers": {
        "php-calculator": {
            "command": "php",
            "args": ["/absolute/path/to/your/server.php"]
        }
    }
}

4. Test Your Server

# Test with MCP Inspector
npx @modelcontextprotocol/inspector php /path/to/server.php

Your AI assistant can now call:

- add: Add two integers

- calculate: Perform arithmetic operations

- Read config://calculator/settings resource

Key Features

Attribute-Based Discovery

Define MCP elements using PHP attributes with automatic discovery:

// Tool with automatic name and description from method
#[McpTool]
public function generateReport(): string { /* ... */ }

// Tool with custom name #[McpTool(name: 'custom_name')] public function myMethod(): string { /* ... */ }

// Resource with URI and metadata #[McpResource(uri: 'config://app/settings', mimeType: 'application/json')] public function getConfig(): array { /* ... */ }

Manual Registration

Register capabilities programmatically:

$server = Server::builder()
    ->addTool([MyClass::class, 'myMethod'], 'tool_name')
    ->addResource([MyClass::class, 'getData'], 'data://config')
    ->build();

Multiple Transport Options

STDIO Transport (Command-line integration):

$transport = new StdioTransport();
$server->run($transport);

HTTP Transport (Web-based communication):

$transport = new StreamableHttpTransport($request, $responseFactory, $streamFactory);
$response = $server->run($transport);
// Handle $response in your web application

Session Management

By default, the SDK uses in-memory sessions. You can configure different session stores:

```php use Mcp\Server\Session\FileSessionStore; use Mcp\Server\Session\InMemorySessionStore; use Mcp\Server\Session\Psr16SessionStore; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\Cache\Adapter\RedisAdapter;

// Use default in-memory sessions with custom TTL $server = Server::builder() ->setSession(ttl: 7200) // 2 hours ->build();

// Override with file-based storage $server = Server::builder() ->setSession(new FileSessionStore(__DIR__ . '/sessions')) ->build();

// Override with in-memory storage and custom TTL $server = Server::builder() ->setSess

Related MCP Servers

AI Research Assistant

AI Research Assistant

hamid-vakilzadeh

AI Research Assistant provides comprehensive access to millions of academic papers through the Semantic Scholar and arXiv databases. This MCP server enables AI coding assistants to perform intelligent literature searches, citation network analysis, and paper content extraction without requiring an API key. Key features include: - Advanced paper search with multi-filter support by year ranges, citation thresholds, field of study, and publication type - Title matching with confidence scoring for finding specific papers - Batch operations supporting up to 500 papers per request - Citation analysis and network exploration for understanding research relationships - Full-text PDF extraction from arXiv and Wiley open-access content (Wiley TDM token required for institutional access) - Rate limits of 100 requests per 5 minutes with options to request higher limits through Semantic Scholar

Web & Search
12 8
Linkup

Linkup

LinkupPlatform

Linkup is a real-time web search and content extraction service that enables AI assistants to search the web and retrieve information from trusted sources. It provides source-backed answers with citations, making it ideal for fact-checking, news gathering, and research tasks. Key features of Linkup: - Real-time web search using natural language queries to find current information, news, and data - Page fetching to extract and read content from any webpage URL - Search depth modes: Standard for direct-answer queries and Deep for complex research across multiple sources - Source-backed results with citations and context from relevant, trustworthy websites - JavaScript rendering support for accessing dynamic content on JavaScript-heavy pages

Web & Search
2 24

context7

huynguyen03dev

5