-
Notifications
You must be signed in to change notification settings - Fork 103
[Client] Feat: Implement MCP client component #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CodeWithKyrian
wants to merge
10
commits into
modelcontextprotocol:main
Choose a base branch
from
CodeWithKyrian:feat/mcp-client-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,784
−70
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a78f6d5
feat(client): add complete MCP client SDK with STDIO and HTTP transports
CodeWithKyrian 015a504
refactor(examples): reorganize examples into server/ and client/ folders
CodeWithKyrian 5155e09
feat: add SamplingRequestHandler for handling server sampling requests
CodeWithKyrian 536c6c0
feat: add setLoggingLevel method to control server logging verbosity
CodeWithKyrian 0c77778
refactor: make progress token dependent on request ID
CodeWithKyrian a82d00c
docs: Update server IP in usage instructions and add notes on samplin…
CodeWithKyrian bcdc258
feat: add complete() method for completion/complete requests
CodeWithKyrian cd545d8
refactor: properly parse and type initialize result
CodeWithKyrian 6a65f43
refactor: standardize method names
CodeWithKyrian 8993ba1
Update composer.json
CodeWithKyrian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||
| # Client Examples | ||||||
|
|
||||||
| These examples demonstrate how to use the MCP PHP Client SDK. | ||||||
|
|
||||||
| ## STDIO Client | ||||||
|
|
||||||
| Connects to an MCP server running as a child process: | ||||||
|
|
||||||
| ```bash | ||||||
| php examples/client/stdio_discovery_calculator.php | ||||||
| ``` | ||||||
|
|
||||||
| ## HTTP Client | ||||||
|
|
||||||
| Connects to an MCP server over HTTP: | ||||||
|
|
||||||
| ```bash | ||||||
| # First, start an HTTP server | ||||||
| php -S localhost:8080 examples/http-discovery-userprofile/server.php | ||||||
|
|
||||||
| # Then run the client | ||||||
| php examples/client/http_discovery_userprofile.php | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## Requirements | ||||||
|
|
||||||
| All examples require the server examples to be available. The STDIO examples spawn the server process, while the HTTP examples connect to a running HTTP server. | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * HTTP Client Communication Example | ||
| * | ||
| * This example demonstrates server-to-client communication over HTTP: | ||
| * - Logging notifications | ||
| * - Progress notifications (via SSE streaming) | ||
| * - Sampling requests (mocked LLM response) | ||
| * | ||
| * Usage: | ||
| * 1. Start the server: php -S 127.0.0.1:8000 examples/server/client-communication/server.php | ||
| * 2. Run this script: php examples/client/http_client_communication.php | ||
| * | ||
| * Note: PHP's built-in server works for listing tools, calling tools, and receiving | ||
| * progress/logging notifications. However, sampling requires a concurrent-capable server | ||
| * (e.g., Symfony CLI, PHP-FPM) since the server must process the client's sampling | ||
| * response while the original tool request is still pending. | ||
| * | ||
| * Eg. symfony serve --passthru=examples/server/client-communication/server.php --no-tls | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| use Mcp\Client\Client; | ||
| use Mcp\Client\Handler\LoggingNotificationHandler; | ||
| use Mcp\Client\Handler\SamplingRequestHandler; | ||
| use Mcp\Client\Transport\HttpClientTransport; | ||
| use Mcp\Schema\ClientCapabilities; | ||
| use Mcp\Schema\Content\TextContent; | ||
| use Mcp\Schema\Enum\Role; | ||
| use Mcp\Schema\Notification\LoggingMessageNotification; | ||
| use Mcp\Schema\Request\CreateSamplingMessageRequest; | ||
| use Mcp\Schema\Result\CreateSamplingMessageResult; | ||
|
|
||
| $endpoint = 'http://127.0.0.1:8000'; | ||
|
|
||
| $loggingNotificationHandler = new LoggingNotificationHandler(function (LoggingMessageNotification $n) { | ||
| echo "[LOG {$n->level->value}] {$n->data}\n"; | ||
| }); | ||
|
|
||
| $samplingRequestHandler = new SamplingRequestHandler(function (CreateSamplingMessageRequest $request): CreateSamplingMessageResult { | ||
| echo "[SAMPLING] Server requested LLM sampling (max {$request->maxTokens} tokens)\n"; | ||
|
|
||
| $mockResponse = "Based on the incident analysis, I recommend: 1) Activate the on-call team, " . | ||
| "2) Isolate affected systems, 3) Begin root cause analysis, 4) Prepare stakeholder communication."; | ||
|
|
||
| return new CreateSamplingMessageResult( | ||
| role: Role::Assistant, | ||
| content: new TextContent($mockResponse), | ||
| model: 'mock-gpt-4', | ||
| stopReason: 'end_turn', | ||
| ); | ||
| }); | ||
|
|
||
| $client = Client::builder() | ||
| ->setClientInfo('HTTP Client Communication Test', '1.0.0') | ||
| ->setInitTimeout(30) | ||
| ->setRequestTimeout(120) | ||
| ->setCapabilities(new ClientCapabilities(sampling: true)) | ||
| ->addNotificationHandler($loggingNotificationHandler) | ||
| ->addRequestHandler($samplingRequestHandler) | ||
| ->build(); | ||
|
|
||
| $transport = new HttpClientTransport(endpoint: $endpoint); | ||
|
|
||
| try { | ||
| echo "Connecting to MCP server at {$endpoint}...\n"; | ||
| $client->connect($transport); | ||
|
|
||
| $serverInfo = $client->getServerInfo(); | ||
| echo "Connected to: " . ($serverInfo?->name ?? 'unknown') . "\n\n"; | ||
|
|
||
| echo "Available tools:\n"; | ||
| $toolsResult = $client->listTools(); | ||
| foreach ($toolsResult->tools as $tool) { | ||
| echo " - {$tool->name}\n"; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "Calling 'run_dataset_quality_checks'...\n\n"; | ||
| $result = $client->callTool( | ||
| name: 'run_dataset_quality_checks', | ||
| arguments: ['dataset' => 'sales_transactions_q4'], | ||
| onProgress: function (float $progress, ?float $total, ?string $message) { | ||
| $percent = $total > 0 ? round(($progress / $total) * 100) : '?'; | ||
| echo "[PROGRESS {$percent}%] {$message}\n"; | ||
| } | ||
| ); | ||
|
|
||
| echo "\nResult:\n"; | ||
| foreach ($result->content as $content) { | ||
| if ($content instanceof TextContent) { | ||
| echo $content->text . "\n"; | ||
| } | ||
| } | ||
|
|
||
| echo "\nCalling 'coordinate_incident_response'...\n\n"; | ||
| $result = $client->callTool( | ||
| name: 'coordinate_incident_response', | ||
| arguments: ['incidentTitle' => 'Database connection pool exhausted'], | ||
| onProgress: function (float $progress, ?float $total, ?string $message) { | ||
| $percent = $total > 0 ? round(($progress / $total) * 100) : '?'; | ||
| echo "[PROGRESS {$percent}%] {$message}\n"; | ||
| } | ||
| ); | ||
|
|
||
| echo "\nResult:\n"; | ||
| foreach ($result->content as $content) { | ||
| if ($content instanceof TextContent) { | ||
| echo $content->text . "\n"; | ||
| } | ||
| } | ||
|
|
||
| } catch (\Throwable $e) { | ||
| echo "Error: {$e->getMessage()}\n"; | ||
| echo $e->getTraceAsString() . "\n"; | ||
| } finally { | ||
| $client->disconnect(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * HTTP Client Example | ||
| * | ||
| * This example demonstrates how to use the MCP client with an HTTP transport | ||
| * to communicate with a remote MCP server over HTTP. | ||
| * | ||
| * Usage: php examples/client/http_discovery_calculator.php | ||
| * | ||
| * Before running, start an HTTP MCP server: | ||
| * php -S localhost:8080 examples/server/http-discovery-calculator/server.php | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
|
||
| use Mcp\Client\Client; | ||
| use Mcp\Client\Transport\HttpClientTransport; | ||
|
|
||
| $endpoint = 'http://localhost:8000'; | ||
|
|
||
| $client = Client::builder() | ||
| ->setClientInfo('HTTP Example Client', '1.0.0') | ||
| ->setInitTimeout(30) | ||
| ->setRequestTimeout(60) | ||
| ->build(); | ||
|
|
||
| $transport = new HttpClientTransport($endpoint); | ||
|
|
||
| try { | ||
| echo "Connecting to MCP server at {$endpoint}...\n"; | ||
| $client->connect($transport); | ||
|
|
||
| echo "Connected! Server info:\n"; | ||
| $serverInfo = $client->getServerInfo(); | ||
| echo " Name: " . ($serverInfo?->name ?? 'unknown') . "\n"; | ||
| echo " Version: " . ($serverInfo?->version ?? 'unknown') . "\n\n"; | ||
|
|
||
| echo "Available tools:\n"; | ||
| $toolsResult = $client->listTools(); | ||
| foreach ($toolsResult->tools as $tool) { | ||
| echo " - {$tool->name}: {$tool->description}\n"; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "Available resources:\n"; | ||
| $resourcesResult = $client->listResources(); | ||
| foreach ($resourcesResult->resources as $resource) { | ||
| echo " - {$resource->uri}: {$resource->name}\n"; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "Available prompts:\n"; | ||
| $promptsResult = $client->listPrompts(); | ||
| foreach ($promptsResult->prompts as $prompt) { | ||
| echo " - {$prompt->name}: {$prompt->description}\n"; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| } catch (\Throwable $e) { | ||
| echo "Error: {$e->getMessage()}\n"; | ||
| echo $e->getTraceAsString() . "\n"; | ||
| } finally { | ||
| echo "Disconnecting...\n"; | ||
| $client->disconnect(); | ||
| echo "Done.\n"; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client example expects port 8000 and server example moved
not sure which one you wanted: