Skip to content

Commit 0dcee0a

Browse files
committed
wip
1 parent add4ce9 commit 0dcee0a

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"require": {
66
"php": "^8.3",
7-
"prism-php/prism": ">=0.56 ^0",
7+
"prism-php/prism": "dev-main",
88
"laravel/framework": "^11.0|^12.0"
99
},
1010
"require-dev": {

src/Relay.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,30 @@ protected function createHandlerFunction(string $toolName, array $definition): c
178178
return fn ($script = null): string => $this->callMCPTool($toolName, ['script' => $script]);
179179
}
180180

181-
// Default generic handler for any other tool
182-
return fn ($parameters = []): string => $this->callMCPTool($toolName, $parameters);
181+
// Default generic handler for any other tool - handle both individual params and array
182+
return function (...$args) use ($toolName, $definition): string {
183+
// Check if we have named parameters (associative array)
184+
if ($args !== [] && array_keys($args) !== range(0, count($args) - 1)) {
185+
// We have named parameters, use them directly
186+
return $this->callMCPTool($toolName, $args);
187+
}
188+
// If first argument is an array, use it as parameters
189+
if (count($args) === 1 && isset($args[0]) && is_array($args[0])) {
190+
return $this->callMCPTool($toolName, $args[0]);
191+
}
192+
193+
// Otherwise, map positional arguments to parameter names
194+
$requiredParams = $this->getRequiredParameters($definition);
195+
$parameters = [];
196+
197+
foreach ($requiredParams as $index => $paramName) {
198+
if (isset($args[$index])) {
199+
$parameters[$paramName] = $args[$index];
200+
}
201+
}
202+
203+
return $this->callMCPTool($toolName, $parameters);
204+
};
183205
}
184206

185207
/**
@@ -319,7 +341,7 @@ protected function addParameter(Tool $tool, string $name, string $type, string $
319341
}
320342

321343
/**
322-
* @param array<string, mixed>|object|string $parameters
344+
* @param array<string|int, mixed>|object|string $parameters
323345
*
324346
* @throws ToolCallException
325347
*/
@@ -350,7 +372,7 @@ protected function extractBaseToolName(string $toolName): string
350372
}
351373

352374
/**
353-
* @param array<string, mixed>|object|string $parameters
375+
* @param array<string|int, mixed>|object|string $parameters
354376
* @return array<string, mixed>
355377
*/
356378
protected function normalizeParameters(string $baseToolName, $parameters): array
@@ -395,11 +417,13 @@ protected function executeMCPToolCall(string $toolName, array $parameters): arra
395417
// MCP requires arguments to be an object, not an array
396418
$normalizedParamsObject = (object) $parameters;
397419

398-
// Call the tool using JSON-RPC format with correct MCP endpoint: tools/call
399-
return $this->transport->sendRequest('tools/call', [
420+
$requestParams = [
400421
'name' => $toolName,
401422
'arguments' => $normalizedParamsObject,
402-
]);
423+
];
424+
425+
// Call the tool using JSON-RPC format with correct MCP endpoint: tools/call
426+
return $this->transport->sendRequest('tools/call', $requestParams);
403427
}
404428

405429
/**

src/Transport/StdioTransport.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ public function close(): void
8181

8282
public function command(): string
8383
{
84+
$command = $this->config['command'];
85+
8486
$parts = array_map(function (string|array $part): string {
8587
if (is_array($part)) {
8688
return addslashes(json_encode($part) ?: '');
8789
}
8890

8991
return $part;
90-
}, $this->config['command']);
92+
}, $command);
9193

9294
return implode(' ', $parts);
9395
}
@@ -196,16 +198,29 @@ protected function sendPingRequest(): void
196198
throw new TransportException('Input stream not initialized');
197199
}
198200

201+
// Send proper MCP initialize request instead of ping
199202
$this->requestId++;
200-
$pingRequest = json_encode([
203+
$initializeRequest = json_encode([
201204
'jsonrpc' => '2.0',
202205
'id' => (string) $this->requestId,
203-
'method' => 'ping',
206+
'method' => 'initialize',
207+
'params' => [
208+
'protocolVersion' => '2024-11-05',
209+
'capabilities' => new \stdClass,
210+
],
204211
]).PHP_EOL;
205-
$this->inputStream->write($pingRequest);
206212

207-
// Reset the request ID as this was just a ping
208-
$this->requestId--;
213+
$this->inputStream->write($initializeRequest);
214+
215+
// Send initialized notification
216+
$initializedNotification = json_encode([
217+
'jsonrpc' => '2.0',
218+
'method' => 'notifications/initialized',
219+
]).PHP_EOL;
220+
221+
$this->inputStream->write($initializedNotification);
222+
223+
// Don't reset the request ID - we expect a response to initialize
209224
}
210225

211226
/**
@@ -246,6 +261,7 @@ protected function prepareRequest(string $method, array $params = []): void
246261
}
247262

248263
$jsonRequest = json_encode($requestPayload, JSON_UNESCAPED_SLASHES).PHP_EOL;
264+
249265
$this->inputStream->write($jsonRequest);
250266
$this->process->clearErrorOutput();
251267
$this->process->clearOutput();

0 commit comments

Comments
 (0)