# mcp-openai

> MCP Client

This repository provides a minimal MCP client library, enabling LLM UIs to support the Model Context Protocol through an OpenAI-compatible API. It facilitates integration with locally runnable inference engines.

## Overview

- **Category:** AI
- **Language:** Python
- **Stars:** 53
- **Forks:** 2
- **Owner:** S1M0N38
- **GitHub:** https://github.com/S1M0N38/mcp-openai
- **Created:** 2024-12-25T00:16:10+00:00
- **Updated:** 2025-03-27T01:36:08+00:00
- **Source:** https://model-context-protocol.com/clients/mcp-client-openai-compatible-api

## Setup

## Setup
It is highly recommended to use [uv](https://docs.astral.sh/uv/) in your project based on mpc-openai.
Add mcp-openai to your project dependencies with:

```
uv add mcp-openai
```

or use classic pip install.

### Create a MCP client
Now you can create a MCP client by specifying your custom configuration.

```python
from mcp_openai import MCPClient
from mcp_openai import config

mcp_client_config = config.MCPClientConfig(
    mcpServers={
        "the-name-of-the-server": config.MCPServerConfig(
            command="uv",
            args=["run", "path/to/server/scripts.py/or/github/raw"],
        )
        # add here other servers ...
    }
)

llm_client_config = config.LLMClientConfig(
    api_key="api-key-for-auth",
    base_url="https://api.openai.com/v1",
)

llm_request_config = config.LLMRequestConfig(model=os.environ["MODEL_NAME"])

client = MCPClient(
    mcp_client_config,
    llm_client_config,
    llm_request_config,
)
```

### Connect and process messages with MCP client

```python
async def main():

    # Establish connection between the client and the server.
    await client.connect_to_server(server_name)

    # messages_in are coming from user interacting with the LLM
    # e.g. UI making use of this MCP client.
    messages_in = ...
    messages_out = await client.process_messages(messages_in)

    # messages_out contains the LLM response. If required, the LLM make use of
    # the available tools offered by the connected servers.
```

## Tools

## Available Tools

1.  MCP Client (library for building LLMs UI that support MCP through an OpenAI compatible API)
2.  OpenAI compatible API (supports text generation, function calling, etc.)
3.  `MCPClient` class (for creating a MCP client with custom configuration)
4.  `connect_to_server` method (establishes connection between the client and the server)
5.  `process_messages` method (processes messages and returns the LLM response)
