opendunes
Coding agents
Copy page ▾

MCP servers

Let a model use the tools of an MCP server, from your own code.

An MCP server offers tools: read a file, query a database, open a ticket. The model never reaches the server itself. Your code starts it, hands its tools to the model in tools, and runs each call the model asks for. The request is the one Tools and function calling sets out; the tools come from the server instead of your own code.

  1. Install the libraries.
  2. Start the server and read its tools.
  3. Run the calls the model asks for.
  4. Read what each turn cost.

1. Install the libraries

The samples start the filesystem MCP server, which reads the files in one folder, with npx: Node must be on your machine whatever language you write in.

TabInstall line
Pythonpip install mcp openai
JavaScriptnpm install @modelcontextprotocol/client openai
Gogo get github.com/modelcontextprotocol/go-sdk/mcp github.com/openai/openai-go/v3
cURLnone: it sends the first turn only

2. Start the server and read its tools

The MCP library starts the server as a child process and lists its tools. Each tool carries a name, a description and a JSON Schema of its arguments: the three parts of a function in tools, under other names.

MCP toolFunction in tools
namefunction.name
descriptionfunction.description
inputSchemafunction.parameters, without its $schema line

The filesystem server offers fourteen tools, and some of them write, move and edit files. The samples pass three that only read.

Don't

Don't hand the model every tool a server offers. It can call any tool it is given, and a call to write_file writes the file.

Do

Pass the tools the job needs, by name, as ALLOWED does below.

3. Run the calls the model asks for

Each turn sends the conversation and the tools to POST /v1/chat/completions. When the answer carries tool_calls, the code runs each call on the server, adds the text it returned as a tool message, and sends the conversation again. When the answer carries words, it prints them and stops. MAX_TURNS caps the loop.

import asyncio
import json
import os
from mcp import Client, StdioServerParameters
from mcp.types import TextContent
from openai import AsyncOpenAI

API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
FOLDER = "."
PROMPT = "Which files in this folder hold Python code?"
MAX_TURNS = 10
ALLOWED = {"list_directory", "read_text_file", "search_files"}

SERVER = StdioServerParameters(
    command="npx",
    args=["-y", "@modelcontextprotocol/server-filesystem", FOLDER],
)

client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)


def as_function(tool):
    parameters = dict(tool.input_schema)
    parameters.pop("$schema", None)
    return {
        "type": "function",
        "function": {
            "name": tool.name,
            "description": tool.description or "",
            "parameters": parameters,
        },
    }


def as_text(result):
    texts = [b.text for b in result.content if isinstance(b, TextContent)]
    return "\n".join(texts)


async def main():
    async with Client(SERVER) as server:
        listed = await server.list_tools()
        tools = [as_function(t) for t in listed.tools if t.name in ALLOWED]
        messages = [{"role": "user", "content": PROMPT}]

        for _ in range(MAX_TURNS):
            reply = await client.chat.completions.create(
                model=MODEL,
                messages=messages,
                tools=tools,
            )
            message = reply.choices[0].message
            print(reply.usage.cost)
            if not message.tool_calls:
                print(message.content)
                break
            messages.append(message)
            for call in message.tool_calls:
                args = json.loads(call.function.arguments)
                result = await server.call_tool(call.function.name, args)
                messages.append(
                    {
                        "role": "tool",
                        "tool_call_id": call.id,
                        "content": as_text(result),
                    }
                )


asyncio.run(main())

cURL speaks no MCP: its tab is the first turn, with one of the server's tools written out by hand. Its answer carries the tool_calls your code runs on the server.

4. Read what each turn cost

Each turn is a request of its own, charged as one, and each sample prints its charge in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges → as it goes. The tools' descriptions and schemas are prompt tokens on every turn, and so is every file the model has read so far. A server with many tools costs more on each turn than the same job with three.

Good to know

A request never names an MCP server. Your code, or your coding agent, runs the server and sends its tools; OpenDunes sees functions in tools, as from any other code.

In a coding agent

A coding agent is an MCP client already: add the server in the agent's own settings, and the agent does steps 2 and 3 for you, on the model you set it up with. Setting each agent up on OpenDunes: Coding agents.

AgentReads MCP servers from
Claude Code.mcp.json at the root of your project, or claude mcp add
Claude Desktopclaude_desktop_config.json, opened from Settings, Developer, Edit Config
Cursor~/.cursor/mcp.json, or .cursor/mcp.json in one project

The OpenDunes docs answer as an MCP server of their own, with no key: MCP · the docs.

Errors

StatusMeansDo
401the key is missing, wrong, paused, revoked or expiredcheck OPENDUNES_API_KEY is set
402the balanceBalanceWhat you can spend, in DA.More on Balance and charges → is at zero or below, cannot pay the request's hold, or the key's cap is spentmake a depositDepositMoney entering your balance, by CIB or Edahabia card.More on Balance and charges →, or raise the cap

A server that fails to start stops the sample before any request, and costs nothing. Every other status: Errors and retries.

Next: Claude Code · Cursor

Last modified on