Skip to content

Commit a8b2c8c

Browse files
committed
feat: server side with http call is ready
1 parent f7bf602 commit a8b2c8c

7 files changed

Lines changed: 430 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import os
3+
from dotenv import load_dotenv
4+
from langchain_mcp_adapters.client import MultiServerMCPClient
5+
from langgraph.prebuilt import create_react_agent
6+
import openai
7+
8+
# remember to start the sse server first
9+
load_dotenv()
10+
openai.api_key = os.getenv("OPENAI_API_KEY")
11+
12+
async def main():
13+
# Connect to HTTP-based MCP server using SSE
14+
client = MultiServerMCPClient({
15+
"demo": {
16+
"url": "http://127.0.0.1:8000/sse",
17+
"transport": "streamable_http"
18+
}
19+
})
20+
21+
# Get available tools from server
22+
tools = await client.get_tools()
23+
print(f"Fetched {len(tools)} tools from MCP server.")
24+
for tool in tools:
25+
print(f"- {tool.name}")
26+
27+
# Create an agent using GPT-4o and those tools
28+
agent = create_react_agent("openai:gpt-4o", tools)
29+
30+
# Prompts to trigger each tool
31+
prompts = [
32+
("about_info", "Call the 'about_info' tool."),
33+
("analysis_scan", "Call the 'analysis_scan' tool on path '../' with display 'matrix'."),
34+
("security_scan", "Call the 'security_scan' tool on path '../'."),
35+
("dependency_scan", "Call the 'dependency_scan' tool on path '../'.")
36+
]
37+
38+
for tool_name, prompt in prompts:
39+
print(f"\nInvoking agent to call '{tool_name}'...")
40+
response = await agent.ainvoke({
41+
"messages": [
42+
{"role": "user", "content": prompt}
43+
]
44+
})
45+
print(f"Result for '{tool_name}':")
46+
print(response)
47+
48+
asyncio.run(main())
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import asyncio
2+
import os
3+
from dotenv import load_dotenv
4+
from langchain_mcp_adapters.client import MultiServerMCPClient
5+
from langgraph.prebuilt import create_react_agent
6+
import openai
7+
8+
# Load .env file
9+
load_dotenv()
10+
openai.api_key = os.getenv("OPENAI_API_KEY")
11+
12+
async def main():
13+
client = MultiServerMCPClient({
14+
"math": {
15+
"command": "python",
16+
"args": ["src/demo_server.py"],
17+
"transport": "stdio"
18+
}
19+
})
20+
21+
tools = await client.get_tools()
22+
print(f"Fetched {len(tools)} tools from MCP server.")
23+
for tool in tools:
24+
print(f"- {tool.name}")
25+
26+
agent = create_react_agent("openai:gpt-4o", tools)
27+
28+
prompts = [
29+
("about_info", "Call the 'about_info' tool."),
30+
("analysis_scan", "Call the 'analysis_scan' tool on path '../' with display 'matrix'."),
31+
("security_scan", "Call the 'security_scan' tool on path '../'."),
32+
("dependency_scan", "Call the 'dependency_scan' tool on path '../'.")
33+
]
34+
35+
for tool_name, prompt in prompts:
36+
print(f"\nInvoking agent to call '{tool_name}'...")
37+
response = await agent.ainvoke({
38+
"messages": [
39+
{"role": "user", "content": prompt}
40+
]
41+
})
42+
print(f"Result for '{tool_name}':")
43+
print(response)
44+
45+
asyncio.run(main())
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import asyncio
2+
import json
3+
from mcp.client.session import ClientSession
4+
from mcp.client.sse import sse_client
5+
from .utils import render_utility_result
6+
7+
# to start the server, run:
8+
# cargo run --release --bin mcp-sse
9+
10+
async def main():
11+
# The URL where the Rust SSE server is listening.
12+
server_url = "http://127.0.0.1:8000/sse"
13+
14+
async with sse_client(server_url) as (read, write):
15+
async with ClientSession(read, write) as session:
16+
await session.initialize()
17+
18+
# List available tools
19+
tools = await session.list_tools()
20+
print("Tools:")
21+
render_utility_result(tools)
22+
23+
# Call the 'about info' tool
24+
about_info_result = await session.call_tool("about_info", {})
25+
print("About info result:")
26+
render_utility_result(about_info_result)
27+
28+
code_analyze_result = await session.call_tool("analysis_scan", {"path": "../", "display": "matrix"})
29+
print("Code analysis result:")
30+
render_utility_result(code_analyze_result)
31+
32+
# Call the 'security scan' tool
33+
security_scan_result = await session.call_tool("security_scan", {"path": "../"})
34+
print("Security scan result:")
35+
render_utility_result(security_scan_result)
36+
37+
# Call the 'dependency scan' tool
38+
dependency_scan_result = await session.call_tool("dependency_scan", {"path": "../"})
39+
print("Dependency scan result:")
40+
render_utility_result(dependency_scan_result)
41+
42+
43+
if __name__ == "__main__":
44+
asyncio.run(main())
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import asyncio
2+
import json
3+
from mcp.client.session import ClientSession
4+
from mcp.client.stdio import StdioServerParameters, stdio_client
5+
from .utils import render_utility_result
6+
7+
# to start the server, run:
8+
# from cpr-rust-server folder cargo run --release
9+
10+
async def main():
11+
async with stdio_client(
12+
StdioServerParameters(command="mcp-stdio")
13+
) as (read, write):
14+
async with ClientSession(read, write) as session:
15+
await session.initialize()
16+
17+
# List available tools
18+
tools = await session.list_tools()
19+
print("Tools:")
20+
render_utility_result(tools)
21+
22+
# Call the 'about info' tool
23+
about_info_result = await session.call_tool("about_info", {})
24+
print("About info result:")
25+
render_utility_result(about_info_result)
26+
27+
code_analyze_result = await session.call_tool("analysis_scan", {"path": "../", "display": "matrix"})
28+
print("Code analysis result:")
29+
render_utility_result(code_analyze_result)
30+
31+
# Call the 'security scan' tool
32+
security_scan_result = await session.call_tool("security_scan", {"path": "../"})
33+
print("Security scan result:")
34+
render_utility_result(security_scan_result)
35+
36+
# Call the 'dependency scan' tool
37+
dependency_scan_result = await session.call_tool("dependency_scan", {"path": "../"})
38+
print("Dependency scan result:")
39+
render_utility_result(dependency_scan_result)
40+
41+
42+
asyncio.run(main())

rust-mcp-server-syncable-cli/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ serde_json = "1.0"
3535
tokio = { version = "1", features = ["full"] }
3636
env_logger = "0.11"
3737
syncable-cli = "0.11.1"
38+
axum = { version = "0.8.4", features = ["json"] }
39+
futures = "0.3.31"
3840

3941
[[bin]]
4042
name = "mcp-stdio"
@@ -44,6 +46,10 @@ path = "src/main.rs"
4446
name = "mcp-sse"
4547
path = "src/main_sse.rs"
4648

49+
[[bin]]
50+
name = "mcp-rest"
51+
path = "src/main_rest.rs"
52+
4753
[dev-dependencies]
4854
assert_cmd = "2"
4955
assert_fs = "1"

0 commit comments

Comments
 (0)