Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python-mcp-client/source-code-final/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $ uv run python -m mcp_client <path/to/mcp/server.py> --chat
```console
$ uv run python -m mcp_client mcp_server/mcp_server.py --chat

MCP Client Started!
MCP Client's Chat Started!
Type your queries or 'quit' to exit.

You: Greet Pythonista
Expand All @@ -68,14 +68,14 @@ You:

The project includes, `mcp_server.py`, which is a minimal MCP server that provides:

- A sample tool that says hello
- A sample `echo` tool that echoes back a message
- Sample prompts and resources

You can use this server to test the client's functionalities.

## Requirements

- Python >= 3.13
- Python >= 3.14
- The MCP Python SDK and OpenAI Python SDK
- An OpenAI API key
- An MCP server to connect to
2 changes: 2 additions & 0 deletions python-mcp-client/source-code-final/mcp_client/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ async def run_chat(handler) -> None:
break

print("\n" + await handler.process_query(query))
except EOFError:
break
except Exception as e:
print(f"\nError: {str(e)}")

Expand Down
8 changes: 4 additions & 4 deletions python-mcp-client/source-code-final/mcp_client/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mcp import ClientSession
from openai import OpenAI

MODEL = "gpt-4o-mini"
MODEL = "gpt-5.4-mini"
MAX_TOKENS = 1000


Expand All @@ -25,7 +25,7 @@ async def process_query(self, query: str) -> str:
messages = [{"role": "user", "content": query}]
initial_response = self.openai.chat.completions.create(
model=MODEL,
max_tokens=MAX_TOKENS,
max_completion_tokens=MAX_TOKENS,
messages=messages,
tools=await self._get_tools(),
)
Expand Down Expand Up @@ -55,7 +55,7 @@ async def process_query(self, query: str) -> str:
# Get final Model's response after tool execution
final_response = self.openai.chat.completions.create(
model=MODEL,
max_tokens=MAX_TOKENS,
max_completion_tokens=MAX_TOKENS,
messages=messages,
)

Expand All @@ -75,7 +75,7 @@ async def _get_tools(self) -> list:
"description": tool.description or "No description",
"parameters": getattr(
tool,
"inputSchema",
"input_schema",
{"type": "object", "properties": {}},
),
},
Expand Down
11 changes: 5 additions & 6 deletions python-mcp-client/source-code-final/mcp_client/mcp_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
from contextlib import AsyncExitStack
from typing import Any, Awaitable, Callable, ClassVar, Self
Expand Down Expand Up @@ -36,13 +37,11 @@ async def _connect_to_server(self) -> ClientSession:
read, write = await self.exit_stack.enter_async_context(
stdio_client(
server=StdioServerParameters(
command="sh",
args=[
"-c",
f"{sys.executable} {self.server_path} 2>/dev/null",
],
command=sys.executable,
args=[self.server_path],
env=None,
)
),
errlog=open(os.devnull, "w"),
)
)
client_session = await self.exit_stack.enter_async_context(
Expand Down
11 changes: 7 additions & 4 deletions python-mcp-client/source-code-final/mcp_server/mcp_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from mcp.server.fastmcp import FastMCP
from pathlib import Path

mcp = FastMCP("mcp_server")
from mcp.server.mcpserver import MCPServer

mcp = MCPServer("mcp_server")


@mcp.tool()
Expand All @@ -12,13 +14,14 @@ async def echo(message: str) -> str:
@mcp.prompt()
async def greeting_prompt(name: str) -> str:
"""A simple greeting prompt."""
return f"Great {name} kindly."
return f"Greet {name} kindly."


@mcp.resource("file://./greeting.txt")
def greeting_file() -> str:
"""The greeting text file."""
with open("greeting.txt", "r", encoding="utf-8") as file:
path = Path(__file__).parent / "greeting.txt"
with open(path, "r", encoding="utf-8") as file:
return file.read()


Expand Down
4 changes: 2 additions & 2 deletions python-mcp-client/source-code-final/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ description = "MCP client for testing MCP servers."
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
"mcp>=1.20.0",
"openai>=2.6.1",
"mcp>=2.2.0,<3.0",
"openai>=3.16.2,<4.0",
]

[project.scripts]
Expand Down
Loading
Loading