How to call mcp server from python?

Updated October 2026 · How we answer

Short answerUse an MCP client library, such as the official Python SDK, to connect to the server, list its tools, and call one by name. The SDK handles the message format so you do not build raw requests yourself.

Install the SDK and pick a transport

The official MCP Python SDK includes a client that handles the handshake and message format for you. Install it with pip, then confirm the current package name in the official docs.

Next, decide how your server runs. A local stdio server is started by a command, while a remote server is reached by its URL.

Connect, list tools, and call one

Open a client session to the server, then call initialize before anything else. Call list_tools to see each tool's name and input schema. Finally, call_tool with the tool name and a dictionary of arguments that matches that schema, then read the returned content.

  • Start the server or note its URL
  • Open a client session
  • Call initialize
  • List tools and read their schemas
  • Call a tool with matching arguments
  • Close the session when you finish

Handle the result

Tool results come back as content items, and text items hold the output you usually want. Check whether the tool reported an error before you use the result.

Wrap the session in a try block so a failed connection still lets your program close cleanly.

Common mistakes

  • Calling a tool before the session has been initialized.
  • Passing arguments that do not match the tool's input schema.
  • Trying to reach a local stdio server by URL instead of starting it with a command.
From our shopsCaseMorph: Type an idea, see a custom phone case in seconds, then print a one-of-one.