This repository was archived by the owner on Mar 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Add tests for template server #7
Closed
jtorreggiani
wants to merge
2
commits into
modelcontextprotocol:main
from
jtorreggiani:jtorreggiani/add-tests-for-template-server
+52
−0
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,50 @@ | ||
import pytest | ||
import os | ||
from mcp import ClientSession, StdioServerParameters | ||
from mcp.client.stdio import stdio_client | ||
from mcp.types import ListResourcesResult, TextContent, TextResourceContents | ||
from pydantic import AnyUrl | ||
|
||
|
||
test_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
@pytest.mark.asyncio(loop_scope='module') | ||
async def test_client_server_interactions(): | ||
"""Fixture that starts the actual server and provides a connected session""" | ||
server_params = StdioServerParameters( | ||
command=f"{test_dir}/template_server/src/template_server/server.py", | ||
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. This path assumes that the server is called template_server, which does not hold true.. |
||
args=[], | ||
env=None | ||
) | ||
async with stdio_client(server_params) as (read, write): | ||
async with ClientSession(read, write) as session: | ||
await session.initialize() | ||
|
||
list_resources_result = await session.list_resources() | ||
assert list_resources_result == ListResourcesResult(nextCursor=None, resources=[]) | ||
|
||
list_tools_result = await session.list_tools() | ||
assert list_tools_result.tools[0].name == 'add-note' | ||
|
||
get_prompt_result = await session.get_prompt(name="summarize-notes") | ||
assert get_prompt_result.description == "Summarize the current notes" | ||
|
||
list_prompts_result = await session.list_prompts() | ||
assert list_prompts_result.prompts[0].name == 'summarize-notes' | ||
|
||
call_tool_result = await session.call_tool("add-note", arguments={ | ||
"name": "example_note", | ||
"content": "Example content" | ||
}) | ||
|
||
tool_content = call_tool_result.content[0] | ||
if isinstance(tool_content, TextContent): | ||
assert tool_content.text == "Added note 'example_note' with content: Example content" | ||
|
||
list_resources_result = await session.list_resources() | ||
resource_uri = list_resources_result.resources[0].uri | ||
assert resource_uri == AnyUrl("note://internal/example_note") | ||
|
||
read_resource_result = await session.read_resource(resource_uri) | ||
if isinstance(read_resource_result.contents[0], TextResourceContents): | ||
assert read_resource_result.contents[0].text== 'Example content' |
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.
I don't think this is the right directory tests should live. This would entangle them with the package.