Before you start testing your MCP server, it’s important to familiarize yourself with the available tools and best practices for debugging. Effective testing ensures your server behaves correctly and helps you quickly find and fix issues. The following section outlines recommended methods for validating your MCP implementation.
This lesson explains how to choose the right testing approach and the most effective testing tool.
By the end of this lesson, you will be able to:
- Explain different testing approaches.
- Use various tools to test your code effectively.
MCP offers tools to help you test and debug your servers:
- MCP Inspector: A command line tool that can be used both as a CLI and as a graphical tool.
- Manual testing: You can use a tool like curl to send web requests, but any HTTP-capable tool will work.
- Unit testing: You can use your preferred testing framework to test features of both server and client.
We’ve covered this tool in earlier lessons, but here’s a brief overview. It’s built with Node.js and you use it by running the npx executable, which downloads and installs the tool temporarily and cleans up after running your request.
The MCP Inspector helps you:
- Discover Server Capabilities: Automatically find available resources, tools, and prompts
- Test Tool Execution: Experiment with different parameters and see responses instantly
- View Server Metadata: Check server info, schemas, and configurations
A typical use of the tool looks like this:
npx @modelcontextprotocol/inspector node build/index.jsThis command starts an MCP server and its visual interface, launching a local web interface in your browser. You’ll see a dashboard showing your registered MCP servers, their available tools, resources, and prompts. The interface lets you interactively test tool execution, inspect server metadata, and view live responses, making it easier to validate and debug your MCP server implementations.
You can also run this tool in CLI mode by adding the --cli option. Here’s an example of running the tool in CLI mode to list all tools on the server:
npx @modelcontextprotocol/inspector --cli node build/index.js --method tools/listBesides using the inspector tool to test server capabilities, another option is to run an HTTP client like curl.
With curl, you can test MCP servers directly by sending HTTP requests:
# Example: Test server metadata
curl http://localhost:3000/v1/metadata
# Example: Execute a tool
curl -X POST http://localhost:3000/v1/tools/execute \
-H "Content-Type: application/json" \
-d '{"name": "calculator", "parameters": {"expression": "2+2"}}'As shown in the curl example above, you send a POST request to invoke a tool with a payload containing the tool’s name and parameters. Choose the method that works best for you. CLI tools are generally faster and can be scripted, which is helpful in CI/CD pipelines.
Create unit tests for your tools and resources to make sure they behave as expected. Here’s some example test code:
import pytest
from mcp.server.fastmcp import FastMCP
from mcp.shared.memory import (
create_connected_server_and_client_session as create_session,
)
# Mark the whole module for async tests
pytestmark = pytest.mark.anyio
async def test_list_tools_cursor_parameter():
"""Test that the cursor parameter is accepted for list_tools.
Note: FastMCP doesn't currently implement pagination, so this test
only verifies that the cursor parameter is accepted by the client.
"""
server = FastMCP("test")
# Create a couple of test tools
@server.tool(name="test_tool_1")
async def test_tool_1() -> str:
"""First test tool"""
return "Result 1"
@server.tool(name="test_tool_2")
async def test_tool_2() -> str:
"""Second test tool"""
return "Result 2"
async with create_session(server._mcp_server) as client_session:
# Test without cursor parameter (omitted)
result1 = await client_session.list_tools()
assert len(result1.tools) == 2
# Test with cursor=None
result2 = await client_session.list_tools(cursor=None)
assert len(result2.tools) == 2
# Test with cursor as string
result3 = await client_session.list_tools(cursor="some_cursor_value")
assert len(result3.tools) == 2
# Test with empty string cursor
result4 = await client_session.list_tools(cursor="")
assert len(result4.tools) == 2
This code does the following:
- Uses the pytest framework, allowing you to write tests as functions and use assert statements.
- Creates an MCP Server with two different tools.
- Uses
assertstatements to verify certain conditions.
Check out the full file here
Using this file, you can test your own server to ensure capabilities are properly created.
All major SDKs include similar testing sections so you can adapt them to your runtime environment.
- Next: Deployment
Penafian:
Dokumen ini telah diterjemahkan menggunakan perkhidmatan terjemahan AI Co-op Translator. Walaupun kami berusaha untuk ketepatan, sila maklum bahawa terjemahan automatik mungkin mengandungi kesilapan atau ketidaktepatan. Dokumen asal dalam bahasa asalnya harus dianggap sebagai sumber yang sahih. Untuk maklumat penting, terjemahan profesional oleh manusia adalah disyorkan. Kami tidak bertanggungjawab atas sebarang salah faham atau salah tafsir yang timbul daripada penggunaan terjemahan ini.
