A Python client for temporal reasoning and learning using Model Context Protocol (MCP) servers
Project description
Temprl MCP Client
A flexible Python library and CLI tool for interacting with Model Context Protocol (MCP) servers using any LLM model.
Overview
Temprl MCP Client is both a Python library and a command-line tool that allows you to query and interact with MCP servers through natural language. It connects to any number of configured MCP servers, makes their tools available to language models (OpenAI, Anthropic, Ollama, LMStudio), and provides a conversational interface for accessing and manipulating data from these servers.
The project demonstrates how to:
- Connect to multiple MCP servers simultaneously
- List and call tools provided by these servers
- Use function calling capabilities to interact with external data sources
- Process and present results in a user-friendly way
- Create a reusable Python library with a clean API
- Build a command-line interface on top of the library
Features
- Multiple Provider Support: Works with OpenAI, Anthropic, Ollama, and LMStudio models
- Modular Architecture: Clean separation of concerns with provider-specific modules
- Dual Interface: Use as a Python library or command-line tool
- MCP Server Integration: Connect to any number of MCP servers simultaneously
- Tool Discovery: Automatically discover and use tools provided by MCP servers
- Flexible Configuration: Configure models and servers through JSON configuration
- Environment Variable Support: Securely store API keys in environment variables
- Comprehensive Documentation: Detailed usage examples and API documentation
- Installable Package: Easy installation via pip with
temprl-mcp-clientcommand
Prerequisites
Before installing Temprl MCP Client, ensure you have the following prerequisites installed:
- Python 3.8+
- SQLite - A lightweight database used by the demo
- uv/uvx - A fast Python package installer and resolver
Setting up Prerequisites
Windows
-
Python 3.8+:
- Download and install from python.org
- Ensure you check "Add Python to PATH" during installation
-
SQLite:
- Download the precompiled binaries from SQLite website
- Choose the "Precompiled Binaries for Windows" section and download the sqlite-tools zip file
- Extract the files to a folder (e.g.,
C:\sqlite) - Add this folder to your PATH:
- Open Control Panel > System > Advanced System Settings > Environment Variables
- Edit the PATH variable and add the path to your SQLite folder
- Verify installation by opening Command Prompt and typing
sqlite3 --version
-
uv/uvx:
- Open PowerShell as Administrator and run:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" - Restart your terminal and verify installation with
uv --version
- Open PowerShell as Administrator and run:
macOS
-
Python 3.8+:
- Install using Homebrew:
brew install python
- Install using Homebrew:
-
SQLite:
- SQLite comes pre-installed on macOS, but you can update it using Homebrew:
brew install sqlite - Verify installation with
sqlite3 --version
- SQLite comes pre-installed on macOS, but you can update it using Homebrew:
-
uv/uvx:
- Install using Homebrew:
brew install uv - Or use the official installer:
curl -LsSf https://astral.sh/uv/install.sh | sh - Verify installation with
uv --version
- Install using Homebrew:
Linux (Ubuntu/Debian)
-
Python 3.8+:
sudo apt update sudo apt install python3 python3-pip -
SQLite:
sudo apt update sudo apt install sqlite3- Verify installation with
sqlite3 --version
- Verify installation with
-
uv/uvx:
curl -LsSf https://astral.sh/uv/install.sh | sh- Verify installation with
uv --version
- Verify installation with
Installation
Option 1: Install from PyPI (Recommended)
pip install temprl-mcp-client
Configuration
The project uses two main configuration files:
-
.env- Contains OpenAI API configuration:OPENAI_API_KEY=your_openai_api_key_here OPENAI_MODEL=gpt-4o # OPENAI_BASE_URL=https://api.openai.com/v1 # Uncomment and modify if using a custom base url -
mcp_config.json- Defines MCP servers to connect to:{ "mcpServers": { "server1": { "command": "command-to-start-server", "args": ["arg1", "arg2"], "env": { "ENV_VAR1": "value1", "ENV_VAR2": "value2" } }, "server2": { "command": "another-server-command", "args": ["--option", "value"] } } }
You can add as many MCP servers as you need, and the client will connect to all of them and make their tools available.
Usage
The Temprl MCP client now supports storing chat memories in a PostgreSQL database, which enables:
- Persistent storage of conversations across sessions
- Loading past conversations by ID
- Continuing conversations from where they left off
Using PostgreSQL for Chat Memory
The chat memory system can be configured to use PostgreSQL for storage, which provides better performance and scalability compared to SQLite.
PostgreSQL Configuration in Code
You can use PostgreSQL directly in your code by passing the appropriate configuration to the ChatMemory class or initialize_mcp function:
from temprl_mcp_client.client import initialize_mcp, run_interaction
# PostgreSQL configuration
postgres_config = {
"host": "localhost",
"port": "5432",
"database": "temprl_mcp",
"user": "postgres",
"password": "your_password"
}
# Initialize MCP with PostgreSQL storage
mcp_manager = await initialize_mcp(
use_postgres=True,
postgres_config=postgres_config
)
# Run interactions as usual
response = await run_interaction(
user_query="Your question here",
mcp_manager=mcp_manager
)
PostgreSQL Configuration via Command Line
You can also configure PostgreSQL from the command line using the provided scripts:
# Initialize a conversation with PostgreSQL storage
python init_conversation.py --use-postgres --pg-host localhost --pg-port 5432 --pg-db temprl_mcp --pg-user postgres --pg-password your_password
# Load an existing conversation from PostgreSQL
python chat_by_id.py --id your-chat-id --use-postgres --pg-host localhost --pg-db temprl_mcp
# Run the test script with PostgreSQL
python test.py --use-postgres --pg-host localhost --pg-user postgres
Using Environment Variables
If you don't provide explicit configuration, the system will look for these environment variables:
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=temprl_mcp
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
You can set these in your environment or in a .env file in your project root.
Setting Up PostgreSQL Database
Before using PostgreSQL, make sure to set up the database:
- Install PostgreSQL on your system
- Create a database for the Temprl MCP client
- Run the setup script:
python setup_postgres.py
This script will create the necessary tables in your PostgreSQL database.
Using Chat Memory
Starting a New Chat
When you start a conversation, the system automatically generates a unique ID for the chat:
from temprl_mcp_client.client import initialize_mcp, run_interaction
# Initialize MCP with a new chat memory
mcp_manager = await initialize_mcp()
# Get the chat ID for later use
chat_id = mcp_manager.chat_memory.chat_id
print(f"New chat created with ID: {chat_id}")
# Run interactions
response = await run_interaction(
user_query="Your question here",
mcp_manager=mcp_manager
)
Loading a Chat by ID
To continue a previous conversation, use the chat ID:
from temprl_mcp_client.client import initialize_mcp, run_interaction
# Load an existing chat by ID
chat_id = "your-previous-chat-id"
mcp_manager = await initialize_mcp(chat_id=chat_id)
# Check if the chat was found
if mcp_manager.chat_memory.is_new:
print(f"No chat found with ID: {chat_id}")
else:
print(f"Loaded chat: {mcp_manager.chat_memory.title}")
# Continue the conversation
response = await run_interaction(
user_query="Your next question",
mcp_manager=mcp_manager
)
Using the Chat ID Tool
The client comes with a convenient tool for managing chats by ID:
# List all available chats
python chat_by_id.py --list
# Start a new chat
python chat_by_id.py
# Load a chat by ID
python chat_by_id.py --id your-chat-id
# Load a chat and immediately send a query
python chat_by_id.py --id your-chat-id --query "Your question here"
Using Specific MCP Servers
By default, all initialized MCP servers are available during the interaction. However, you can now selectively use only certain servers for specific interactions:
from temprl_mcp_client.client import initialize_mcp, run_interaction
# Initialize all MCP servers first
mcp_manager = await initialize_mcp()
# Get list of available servers
available_servers = mcp_manager.get_available_servers()
print(f"Available servers: {available_servers}") # e.g., ['zoom', 'gmail', 'business']
# Use only specific servers for this interaction
response = await run_interaction(
user_query="Schedule a Zoom meeting for tomorrow",
mcp_manager=mcp_manager,
server_names=["zoom"] # Only use the Zoom server for this interaction
)
# In another interaction, use different servers
response = await run_interaction(
user_query="Check my unread emails",
mcp_manager=mcp_manager,
server_names=["gmail"] # Only use the Gmail server for this interaction
)
# You can also use multiple servers in one interaction
response = await run_interaction(
user_query="Create a meeting and send confirmation emails",
mcp_manager=mcp_manager,
server_names=["zoom", "gmail"] # Use both Zoom and Gmail servers
)
This approach allows you to:
- Initialize all servers once at the beginning
- Selectively use only the servers needed for each specific interaction
- Reduce the number of tools exposed to the model, improving its performance
Using Custom System Prompts and User IDs
You can now provide a custom system prompt for each interaction and associate user IDs with interactions:
from temprl_mcp_client.client import initialize_mcp, run_interaction
# Initialize MCP manager
mcp_manager = await initialize_mcp()
# Run an interaction with a custom system prompt
response = await run_interaction(
user_query="What can you tell me about machine learning?",
mcp_manager=mcp_manager,
system_prompt="You are an AI expert specialized in machine learning and data science. Provide detailed, technical answers."
)
# Run an interaction with a user ID
response = await run_interaction(
user_query="Schedule a meeting with my team",
mcp_manager=mcp_manager,
user_id="user123" # This ID will be passed to MCP servers
)
# Combine all parameters
response = await run_interaction(
user_query="Analyze my sales data and schedule a presentation",
mcp_manager=mcp_manager,
server_names=["analytics", "calendar"], # Only use these specific servers
system_prompt="You are a sales analyst assistant. Focus on business insights.",
user_id="sales_manager_42" # This ID will be passed to MCP servers
)
Benefits of these features:
- Custom System Prompts: Override the default system prompt for specific interactions to tailor the model's behavior
- User ID Support: Pass user identifiers to MCP servers for authentication, personalization, or tracking
- Flexible Integration: Combine with server selection for highly customized interactions
From the command line, you can use these features with:
# Use a custom system prompt
python init_conversation.py --system-prompt "You are a specialized assistant for scientific research" --query "Explain quantum computing"
# Associate a user ID with the interaction
python init_conversation.py --user-id "researcher_42" --query "Search for recent papers on CRISPR"
# Combine all options
python init_conversation.py --servers "research,calendar" --system-prompt "You are a research assistant" --user-id "lab_director" --query "Schedule a lab meeting and find relevant papers"
API Reference
The chat memory system provides the following features:
ChatMemory(chat_id=None)- Create or load a chat memorychat_memory.chat_id- Get the unique ID for the current chatChatMemory.list_conversations()- List all available chatsChatMemory.delete_conversation(chat_id)- Delete a chat by ID
The system automatically persists all messages to the database as you chat, so there's no need to manually save the state.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file temprl_mcp_client-0.1.4.tar.gz.
File metadata
- Download URL: temprl_mcp_client-0.1.4.tar.gz
- Upload date:
- Size: 38.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a05fbe51e8cbafd0c8c913028dca9afa22e0dbf1408ec888ccfe205f2c9bc98
|
|
| MD5 |
d0b03227c36324f0d834e3897a244b12
|
|
| BLAKE2b-256 |
4cff6484abf667304d29daca3a8c11bf79ba0567f1c6b4eea588960bcdc2baf2
|
File details
Details for the file temprl_mcp_client-0.1.4-py3-none-any.whl.
File metadata
- Download URL: temprl_mcp_client-0.1.4-py3-none-any.whl
- Upload date:
- Size: 38.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3df9144ac21e0e4558025a216190f78b44662b3c2e5632858f49c86ab16035c
|
|
| MD5 |
40a5209795b3e8eeb0b02a4e18ee281c
|
|
| BLAKE2b-256 |
9d498b34e3b4c50394f5208e9ae4c30551cd03f07e836d0246a636a0610bb25e
|