Skip to content

Commit 9c766fe

Browse files
committed
Merge branch 'master' into python-bytes-to-strings
2 parents 77c9819 + 00b3ede commit 9c766fe

File tree

19 files changed

+1009
-1
lines changed

19 files changed

+1009
-1
lines changed

how-to-indent-in-python/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Properly Indent Python Code
2+
3+
This folder contains sample code for the Real Python tutorial [How to Properly Indent Python Code](https://realpython.com/how-to-indent-in-python/).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
long_list_of_names = [
2+
"Amy",
3+
"Brian",
4+
"Carol",
5+
"Dennis",
6+
"Emma",
7+
"Frank",
8+
"Georgia",
9+
"Herbert",
10+
"Isabelle",
11+
"Joshua",
12+
"Kimberly",
13+
"Laurence",
14+
"Megan",
15+
"Nicolas",
16+
"Ophelia",
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lucky_number = 7
2+
for number in range(10):
3+
if number == lucky_number:
4+
print("Found the lucky number!")
5+
else:
6+
print(f"{number} is not my lucky number.")
7+
8+
print("Done.")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ruff: noqa
2+
def add(a, b):
3+
answer = a + b
4+
5+
return answer
6+
7+
def sub (c ,
8+
d):
9+
10+
answer = c -d
11+
12+
return answer

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ line-length = 79
44
exclude = [
55
".devcontainers",
66
".github",
7-
"migrations"
7+
"migrations",
8+
"how-to-indent-in-python/sample_code.py"
89
]
910

1011
[tool.ruff.lint]

python-mcp-client/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Build a Python MCP Client to Test Servers From Your Terminal
2+
3+
This folder provides the code examples for the Real Python article [Build a Python MCP Client to Test Servers From Your Terminal](https://realpython.com/python-mcp-client/).
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# MCP Client
2+
3+
A minimal client for testing Model Context Protocol (MCP) servers, featuring AI integration.
4+
5+
## Features
6+
7+
- **MCP server integration**: Connect to any MCP server
8+
- **Server introspection**: List available tools, prompts, and resources with the `--members` option
9+
- **TUI AI-powered chat**: Interactive chat with AI-powered tool execution using the `--chat` option
10+
11+
## Installation
12+
13+
1. Download the sample code
14+
15+
2. Install dependencies:
16+
```console
17+
$ uv sync
18+
```
19+
20+
3. Set up your OpenAI API key:
21+
```console
22+
$ export OPENAI_API_KEY="your-openai-api-key"
23+
```
24+
25+
## Usage
26+
27+
### List Server Members
28+
29+
Inspect what tools, prompts, and resources are available on an MCP server:
30+
31+
```console
32+
$ uv run python -m mcp_client /path/to/mcp_server.py --members
33+
```
34+
35+
This command will display the following:
36+
37+
- Tools and their descriptions
38+
- Prompts and their purposes
39+
- Resources and their types
40+
41+
### Interactive Chat Mode
42+
43+
Start an interactive chat session using the MCP server tools:
44+
45+
```console
46+
$ uv run python -m mcp_client <path/to/mcp/server.py> --chat
47+
```
48+
49+
- Example - Ask questions and get AI-powered responses:
50+
```console
51+
$ uv run python -m mcp_client mcp_server/mcp_server.py --chat
52+
53+
MCP Client Started!
54+
Type your queries or 'quit' to exit.
55+
56+
You: Greet Pythonista
57+
58+
Assistant: [Used echo({'message': "Hello, Pythonista! 🐍 How's your coding journey going today?"})]
59+
Hello, Pythonista! 🐍 How's your coding journey going today?
60+
61+
You:
62+
```
63+
64+
- The AI will automatically use available MCP tools when needed
65+
- Type `quit` to exit
66+
67+
## Example MCP Server
68+
69+
The project includes, `mcp_server.py`, which is a minimal MCP server that provides:
70+
71+
- A sample tool that says hello
72+
- Sample prompts and resources
73+
74+
You can use this server to test the client's functionalities.
75+
76+
## Requirements
77+
78+
- Python >= 3.13
79+
- The MCP Python SDK and OpenAI Python SDK
80+
- An OpenAI API key
81+
- An MCP server to connect to

python-mcp-client/source-code-final/mcp_client/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import asyncio
2+
3+
from mcp_client.cli import parse_args
4+
from mcp_client.mcp_client import MCPClient
5+
6+
7+
async def main() -> None:
8+
"""Run the MCP client with the specified options."""
9+
args = parse_args()
10+
if not args.server_path.exists():
11+
print(f"Error: Server script '{args.server_path}' not found")
12+
return
13+
14+
try:
15+
async with MCPClient(str(args.server_path)) as client:
16+
if args.members:
17+
await client.list_all_members()
18+
elif args.chat:
19+
await client.run_chat()
20+
except RuntimeError as e:
21+
print(e)
22+
23+
24+
def cli_main():
25+
"""Entry point for the mcp-client CLI app."""
26+
asyncio.run(main())
27+
28+
29+
if __name__ == "__main__":
30+
asyncio.run(main())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
async def run_chat(handler) -> None:
2+
"""Run an AI-handled chat session."""
3+
print("\nMCP Client's Chat Started!")
4+
print("Type your queries or 'quit' to exit.")
5+
6+
while True:
7+
try:
8+
if not (query := input("\nYou: ").strip()):
9+
continue
10+
if query.lower() == "quit":
11+
break
12+
13+
print("\n" + await handler.process_query(query))
14+
except Exception as e:
15+
print(f"\nError: {str(e)}")
16+
17+
print("\nGoodbye!")

0 commit comments

Comments
 (0)