mcp tutorial
This commit is contained in:
parent
674240c90b
commit
d359ce486a
|
|
@ -1,59 +1,51 @@
|
||||||
# Simple PocketFlow Chat
|
# PocketFlow MCP Demo
|
||||||
|
|
||||||
A basic chat application using PocketFlow with OpenAI's GPT-4o model.
|
This project shows how to build an agent that performs addition using PocketFlow and Model Context Protocol (MCP). It presents a comparison between using MCP and basic function calling approaches.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Conversational chat interface in the terminal
|
- Addition operations through a simple terminal interface
|
||||||
- Maintains full conversation history for context
|
- Integration with Model Context Protocol (MCP)
|
||||||
- Simple implementation demonstrating PocketFlow's node and flow concepts
|
- Comparison between MCP and direct function calling
|
||||||
|
|
||||||
## Run It
|
## How to Run
|
||||||
|
|
||||||
1. Make sure your OpenAI API key is set:
|
1. Set your API key:
|
||||||
```bash
|
```bash
|
||||||
export OPENAI_API_KEY="your-api-key-here"
|
export OPENAI_API_KEY="your-api-key-here"
|
||||||
```
|
```
|
||||||
Alternatively, you can edit the `utils.py` file to include your API key directly.
|
Or update it directly in `utils.py`
|
||||||
|
|
||||||
2. Install requirements and run the application:
|
2. Install and run:
|
||||||
```bash
|
```bash
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
python main.py
|
python main.py
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## MCP vs Basic Function Calling
|
||||||
|
|
||||||
## Run the MCP
|
### Basic Function Calling
|
||||||
|
- Functions are directly embedded in application code
|
||||||
|
- Each new tool requires modifying the application
|
||||||
|
- Tools are defined within the application itself
|
||||||
|
|
||||||
Run the server
|
### MCP Approach
|
||||||
|
- Tools live in separate MCP servers
|
||||||
```bash
|
- Standard protocol for all tool interactions
|
||||||
python simple_server.py
|
- New tools can be added without changing the agent
|
||||||
```
|
- AI can interact with tools through a consistent interface
|
||||||
|
|
||||||
Run the client
|
|
||||||
|
|
||||||
```bash
|
|
||||||
python simple_client.py
|
|
||||||
```
|
|
||||||
|
|
||||||
## How It Works
|
## How It Works
|
||||||
|
|
||||||
```mermaid
|
|
||||||
flowchart LR
|
|
||||||
chat[ChatNode] -->|continue| chat
|
|
||||||
```
|
|
||||||
|
|
||||||
The chat application uses:
|
|
||||||
- A single `ChatNode` with a self-loop that:
|
|
||||||
- Takes user input in the `prep` method
|
|
||||||
- Sends the complete conversation history to GPT-4o
|
|
||||||
- Adds responses to the conversation history
|
|
||||||
- Loops back to continue the chat until the user types 'exit'
|
|
||||||
|
|
||||||
|
The agent uses PocketFlow to create a workflow where:
|
||||||
|
1. It takes user input about numbers
|
||||||
|
2. Connects to the MCP server for addition operations
|
||||||
|
3. Returns the result
|
||||||
|
|
||||||
## Files
|
## Files
|
||||||
|
|
||||||
- [`main.py`](./main.py): Implementation of the ChatNode and chat flow
|
- [`main.py`](./main.py): Implementation of the addition agent using PocketFlow
|
||||||
- [`utils.py`](./utils.py): Simple wrapper for calling the OpenAI API
|
- [`utils.py`](./utils.py): Helper functions for API calls and MCP integration
|
||||||
|
- [`simple_server.py`](./simple_server.py): MCP server that provides the addition tool
|
||||||
|
- [`simple_client.py`](./simple_client.py): Example client that connects to the MCP server
|
||||||
Loading…
Reference in New Issue