Skip to content

Commit ffc132c

Browse files
author
larry.sulebalogun
committed
10 - Update the ListPromptsHandler to use annotations
1 parent 67a5c2a commit ffc132c

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/Server/RequestHandler/ListPromptsHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Mcp\Server\RequestHandler;
1313

1414
use Mcp\Capability\Registry;
15+
use Mcp\Exception\InvalidCursorException;
1516
use Mcp\Schema\JsonRpc\HasMethodInterface;
1617
use Mcp\Schema\JsonRpc\Response;
1718
use Mcp\Schema\Request\ListPromptsRequest;
@@ -34,6 +35,9 @@ public function supports(HasMethodInterface $message): bool
3435
return $message instanceof ListPromptsRequest;
3536
}
3637

38+
/**
39+
* @throws InvalidCursorException
40+
*/
3741
public function handle(ListPromptsRequest|HasMethodInterface $message): Response
3842
{
3943
\assert($message instanceof ListPromptsRequest);

tests/Server/RequestHandler/ListPromptsHandlerTest.php

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public function testReturnsFirstPageWhenNoCursorProvided(): void
4242
$response = $this->handler->handle($request);
4343

4444
// Assert
45-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
46-
$this->assertCount(3, $response->result->prompts);
47-
$this->assertNotNull($response->result->nextCursor);
48-
49-
$this->assertEquals('prompt_0', $response->result->prompts[0]->name);
50-
$this->assertEquals('prompt_1', $response->result->prompts[1]->name);
51-
$this->assertEquals('prompt_2', $response->result->prompts[2]->name);
45+
/** @var ListPromptsResult $result */
46+
$result = $response->result;
47+
$this->assertInstanceOf(ListPromptsResult::class, $result);
48+
$this->assertCount(3, $result->prompts);
49+
$this->assertNotNull($result->nextCursor);
50+
51+
$this->assertEquals('prompt_0', $result->prompts[0]->name);
52+
$this->assertEquals('prompt_1', $result->prompts[1]->name);
53+
$this->assertEquals('prompt_2', $result->prompts[2]->name);
5254
}
5355

5456
#[TestDox('Returns paginated prompts with cursor')]
@@ -62,13 +64,15 @@ public function testReturnsPaginatedPromptsWithCursor(): void
6264
$response = $this->handler->handle($request);
6365

6466
// Assert
65-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
66-
$this->assertCount(3, $response->result->prompts);
67-
$this->assertNotNull($response->result->nextCursor);
68-
69-
$this->assertEquals('prompt_0', $response->result->prompts[0]->name);
70-
$this->assertEquals('prompt_1', $response->result->prompts[1]->name);
71-
$this->assertEquals('prompt_2', $response->result->prompts[2]->name);
67+
/** @var ListPromptsResult $result */
68+
$result = $response->result;
69+
$this->assertInstanceOf(ListPromptsResult::class, $result);
70+
$this->assertCount(3, $result->prompts);
71+
$this->assertNotNull($result->nextCursor);
72+
73+
$this->assertEquals('prompt_0', $result->prompts[0]->name);
74+
$this->assertEquals('prompt_1', $result->prompts[1]->name);
75+
$this->assertEquals('prompt_2', $result->prompts[2]->name);
7276
}
7377

7478
#[TestDox('Returns second page with cursor')]
@@ -85,13 +89,15 @@ public function testReturnsSecondPageWithCursor(): void
8589
$response = $this->handler->handle($secondPageRequest);
8690

8791
// Assert
88-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
89-
$this->assertCount(3, $response->result->prompts);
90-
$this->assertNotNull($response->result->nextCursor);
91-
92-
$this->assertEquals('prompt_3', $response->result->prompts[0]->name);
93-
$this->assertEquals('prompt_4', $response->result->prompts[1]->name);
94-
$this->assertEquals('prompt_5', $response->result->prompts[2]->name);
92+
/** @var ListPromptsResult $result */
93+
$result = $response->result;
94+
$this->assertInstanceOf(ListPromptsResult::class, $result);
95+
$this->assertCount(3, $result->prompts);
96+
$this->assertNotNull($result->nextCursor);
97+
98+
$this->assertEquals('prompt_3', $result->prompts[0]->name);
99+
$this->assertEquals('prompt_4', $result->prompts[1]->name);
100+
$this->assertEquals('prompt_5', $result->prompts[2]->name);
95101
}
96102

97103
#[TestDox('Returns last page with null cursor')]
@@ -108,12 +114,14 @@ public function testReturnsLastPageWithNullCursor(): void
108114
$response = $this->handler->handle($secondPageRequest);
109115

110116
// Assert
111-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
112-
$this->assertCount(2, $response->result->prompts);
113-
$this->assertNull($response->result->nextCursor);
114-
115-
$this->assertEquals('prompt_3', $response->result->prompts[0]->name);
116-
$this->assertEquals('prompt_4', $response->result->prompts[1]->name);
117+
/** @var ListPromptsResult $result */
118+
$result = $response->result;
119+
$this->assertInstanceOf(ListPromptsResult::class, $result);
120+
$this->assertCount(2, $result->prompts);
121+
$this->assertNull($result->nextCursor);
122+
123+
$this->assertEquals('prompt_3', $result->prompts[0]->name);
124+
$this->assertEquals('prompt_4', $result->prompts[1]->name);
117125
}
118126

119127
#[TestDox('Handles empty registry')]
@@ -126,9 +134,11 @@ public function testHandlesEmptyRegistry(): void
126134
$response = $this->handler->handle($request);
127135

128136
// Assert
129-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
130-
$this->assertCount(0, $response->result->prompts);
131-
$this->assertNull($response->result->nextCursor);
137+
/** @var ListPromptsResult $result */
138+
$result = $response->result;
139+
$this->assertInstanceOf(ListPromptsResult::class, $result);
140+
$this->assertCount(0, $result->prompts);
141+
$this->assertNull($result->nextCursor);
132142
}
133143

134144
#[TestDox('Throws exception for invalid cursor')]
@@ -172,9 +182,11 @@ public function testHandlesCursorAtExactBoundary(): void
172182
$response = $this->handler->handle($request);
173183

174184
// Assert
175-
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
176-
$this->assertCount(0, $response->result->prompts);
177-
$this->assertNull($response->result->nextCursor);
185+
/** @var ListPromptsResult $result */
186+
$result = $response->result;
187+
$this->assertInstanceOf(ListPromptsResult::class, $result);
188+
$this->assertCount(0, $result->prompts);
189+
$this->assertNull($result->nextCursor);
178190
}
179191

180192
#[TestDox('Maintains stable cursors across calls')]

0 commit comments

Comments
 (0)