0Pricing
MCP Academy · Lesson

Invoke Tools from Code

Call a server tool and read its result.

Invoke Tools from Code is a free MCP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Listing to Doing

Discovery told you what exists. Now you actually invoke a tool, sending arguments and reading back the result from your own code.

Call by Name

Use session.call_tool() with the tool name and a dictionary of arguments. The names must match the inputSchema you discovered.

result = await session.call_tool(
    "add",
    {"a": 2, "b": 3},
)

Arguments Must Validate

Your arguments dictionary should satisfy the tool inputSchema. Sending the wrong shape leads the server to reject the call.

await session.call_tool("greet", {"name": "Ada"})

Read the Content

The result carries a content list of blocks. Most tools return text, so you read the text off the first block.

print(result.content[0].text)

Check for Errors

A tool that fails sets isError to true on the result instead of raising. Inspect it so your client can react gracefully.

if result.isError:
    print("tool failed")

Structured Output Too

Newer tools may also return structuredContent, a typed object alongside the text, when the server defines an output schema.

print(result.structuredContent)

Pass Real Arguments

Arguments come from your program, a user, or a model. The client simply forwards them as a plain dict to the named tool.

args = {"city": "Paris"}
await session.call_tool("weather", args)

It Is Still Async

Like every network call, call_tool is awaited. The await pauses until the server finishes the work and the result returns.

One Call, One Result

Each invocation is a request-response pair. The session matches the reply to your request by id, so results never get crossed.

Handle Slow Tools

Some tools take time. A robust client sets a timeout so a hung tool does not freeze your whole program.

You Are the Orchestrator

Calling tools from code means your program decides the order, combines results, and turns raw output into something useful for the user.

Quick Check

How do you run a discovered tool from client code?

Recap: Making the Call

You invoked tools end to end: call by name with valid arguments, read the content blocks, and check isError before trusting the output.

Frequently asked questions

Is the “Invoke Tools from Code” lesson free?

Yes — the full text of “Invoke Tools from Code” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Invoke Tools from Code”?

Call a server tool and read its result. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Invoke Tools from Code” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Open a Client Session
  2. Discover Tools & Resources
  3. Invoke Tools from Code
  4. Route Tool Calls Through an LLM
← Back to MCP Academy