Connect to Any MCP Server in TypeScript Over stdio and HTTP, Then List Its Tools
Most people meet the Model Context Protocol (MCP) through a client like Claude Desktop, where a config file quietly wires up servers and you never see the handshake. But the protocol is just a client and a server talking over a transport, and the official TypeScript SDK gives you every piece you need to do that yourself. In this tutorial I want to show you how to connect to an MCP server…
Connecting to an MCP server in TypeScript is achievable using either stdio or HTTP transports, with the official TypeScript SDK providing all necessary components to perform these tasks. This tutorial demonstrates how to establish connections over both interfaces and enumerate the server's tools, resources, and prompts. By leveraging the SDK, developers can easily write their own clients, create server indexes, or scan servers for trustworthiness.
The tutorial grounds the code in mcp-audit, a security scanner built for this purpose, although the technique is reusable for various use cases. The protocol comprises three core elements: a transport that carries bytes over stdin/stdout (stdio) or HTTP, a Client that communicates the MCP protocol over the chosen transport, and capability calls like listTools() asking the server for its offerings.
The SDK design allows the Client to be transport-agnostic, so developers only need to build the transport and integrate it into their code. To get started, install the SDK package and ensure Node 18 or newer is installed, setting `type: "module"` in package.json as the SDK ships as ESM. The first transport, StdioClientTransport, enables connections to local servers by spawning a child process with the provided command and options.
It handles MCP messages sent to stdout, while stderr captures server logs to prevent them from corrupting the stream. For remote servers, StreamableHTTPClientTransport or SSEClientTransport can be used, both of which inherit from the Transport interface, allowing the same probe function to operate on either transport. The probe function accepts any Transport object, connects to the server, retrieves the server version, and enumerates tools offered by the server, demonstrating the power and flexibility of the MCP protocol.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.