Skip to content

Commit 827e784

Browse files
committed
Add stdio transport testing capabilities and update documentation
- Added a new Python example script (publish_example.py) for testing the MCP server using stdio transport. - Introduced a requirements.txt file for Python dependencies needed for the example. - Created a comprehensive guide (stdio_example.md) detailing how to test the MCP server locally with stdio transport, including prerequisites and usage instructions. - Updated README.md to include information on testing with stdio transport and revised the NATS_URL environment variable description for clarity. - Added venv directory to .gitignore to prevent virtual environment files from being tracked.
1 parent f16f744 commit 827e784

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode/*
22
.cursor/*
33
mcp-nats
4+
venv/*

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ go build -o mcp-nats ./cmd/mcp-nats
7979
## Configuration
8080

8181
### Environment Variables
82-
- `NATS_URL`: The URL of your NATS server (e.g., `nats://localhost:4222`)
82+
- `NATS_URL`: The URL of your NATS server (e.g., `localhost:4222`)
8383
- `NATS_<ACCOUNT>_CREDS`: Base64 encoded NATS credentials for each account
8484
- Example: `NATS_SYS_CREDS`, `NATS_A_CREDS`
8585

@@ -193,6 +193,10 @@ make run-sse # Run with SSE transport
193193
make lint # Run linters
194194
```
195195

196+
## Testing with stdio Transport
197+
198+
For detailed instructions on how to test the MCP server using stdio transport, please refer to our [Stdio Example Guide](docs/stdio/stdio_example.md).
199+
196200
## Resources
197201
- [Model Context Protocol Documentation](https://modelcontextprotocol.io/introduction)
198202
- [MCP Specification](https://modelcontextprotocol.io)

docs/stdio/publish_example.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# publish_example.py
2+
import asyncio
3+
from mcp_client import StdioServerParameters, stdio_client, ClientSession
4+
5+
async def main():
6+
# Configure the server parameters
7+
server_params = StdioServerParameters(
8+
command="./mcp-nats",
9+
args=["--transport", "stdio"],
10+
env={
11+
"NATS_URL": "Your NATS server URL", # Replace with your NATS server URL
12+
"NATS_A_CREDS": "Your base64 encoded credentials", # Replace with your credentials
13+
}
14+
)
15+
16+
# Create the connection via stdio transport
17+
async with stdio_client(server_params) as streams:
18+
# Create the client session with the streams
19+
async with ClientSession(*streams) as session:
20+
# Initialize the session
21+
await session.initialize()
22+
23+
# List available tools to verify publish tool is available
24+
response = await session.list_tools()
25+
print("Available tools:", [tool.name for tool in response.tools])
26+
27+
# Example 1: Simple message publish
28+
result = await session.call_tool("publish", {
29+
"account_name": "A",
30+
"subject": "test.message",
31+
"body": "Hello from test message!"
32+
})
33+
print("Publish result:", result.content)
34+
35+
if __name__ == "__main__":
36+
asyncio.run(main())

docs/stdio/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mcp>=0.26.0
2+
anthropic>=0.8.0
3+
python-dotenv>=1.0.0
4+
asyncio

docs/stdio/stdio_example.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Testing MCP Server using stdio Transport
2+
3+
This guide explains how to test the MCP server locally using stdio transport format.
4+
5+
## Prerequisites
6+
7+
- Go (for building the server)
8+
- Python 3.x (for running test client)
9+
- NATS credentials
10+
11+
## Quick Start
12+
13+
1. Clone the repository:
14+
```bash
15+
git clone https://github.com/sinadarbouy/mcp-nats.git
16+
cd mcp-nats
17+
```
18+
19+
2. Build the server:
20+
```bash
21+
go build -o mcp-nats ./cmd/mcp-nats
22+
chmod +x mcp-nats
23+
```
24+
25+
## Testing Methods
26+
27+
### Using Cursor IDE
28+
29+
If you're using Cursor IDE, you can configure the MCP server in your settings:
30+
31+
```json
32+
{
33+
"mcpServers": {
34+
"MCP_NATS_STDIO": {
35+
"command": "./mcp-nats",
36+
"args": ["--transport", "stdio"],
37+
"env": {
38+
"NATS_URL": "Your NATS server URL",
39+
"NATS_A_CREDS": "<base64 of A account creds>"
40+
}
41+
}
42+
}
43+
}
44+
```
45+
46+
After configuration, you can interact with the server directly through Cursor using natural language commands. For example:
47+
48+
1. You can tell Cursor:
49+
```
50+
"publish dummy message to A account"
51+
```
52+
53+
2. Cursor will automatically translate this to an MCP tool call:
54+
```json
55+
{
56+
"account_name": "A",
57+
"subject": "test.message",
58+
"body": "Hello from test message!"
59+
}
60+
```
61+
62+
3. The result will be displayed:
63+
```
64+
"Published 1 message(s) to test.message"
65+
```
66+
67+
This natural language interface makes it easy to interact with the MCP server without needing to remember specific command formats or tool names.
68+
69+
### Using Python Test Client
70+
71+
If you're not using Cursor, you can test the server using the provided Python client:
72+
73+
1. Set up Python environment:
74+
```bash
75+
python -m venv venv
76+
source venv/bin/activate
77+
pip install --upgrade pip
78+
pip install -r docs/stdio/requirements.txt
79+
```
80+
81+
2. Run the example client (publish_example.py):
82+
```python
83+
from mcp_client import StdioServerParameters, stdio_client, ClientSession
84+
85+
async def main():
86+
server_params = StdioServerParameters(
87+
command="./mcp-nats",
88+
args=["--transport", "stdio"],
89+
env={
90+
"NATS_URL": "Your NATS server URL",
91+
"NATS_A_CREDS": "Your base64 encoded credentials",
92+
}
93+
)
94+
95+
async with stdio_client(server_params) as streams:
96+
async with ClientSession(*streams) as session:
97+
await session.initialize()
98+
99+
# List available tools
100+
response = await session.list_tools()
101+
print("Available tools:", [tool.name for tool in response.tools])
102+
103+
# Example: Publish a message
104+
result = await session.call_tool("publish", {
105+
"account_name": "A",
106+
"subject": "test.message",
107+
"body": "Hello from test message!"
108+
})
109+
print("Publish result:", result.content)
110+
111+
if __name__ == "__main__":
112+
asyncio.run(main())
113+
```
114+
115+
3. Execute the example:
116+
```bash
117+
python docs/stdio/publish_example.py
118+
```
119+
120+
## Expected Output
121+
122+
When running the example client, you should see output similar to:
123+
124+
```
125+
Available tools: ['account_backup', 'account_info', 'account_report_connections', ...]
126+
Publish result: [TextContent(type='text', text='Published 1 message(s) to test.message')]
127+
```
128+
129+
## Environment Variables
130+
131+
- `NATS_URL`: The URL of your NATS server
132+
- `NATS_A_CREDS`: Base64 encoded credentials for the A account
133+
134+
## Notes
135+
136+
- Make sure to replace placeholder values (NATS_URL, NATS_A_CREDS) with your actual configuration
137+
- The server must be built and executable before running any tests
138+
- All credentials should be properly base64 encoded

0 commit comments

Comments
 (0)