Skip to main content

Overview

The basic TypeScript client demonstrates how to connect to an MCP server and interact with its capabilities including tools, resources, and prompts. This implementation uses the official @modelcontextprotocol/sdk package.

Source Code

The implementation can be found at ~/workspace/source/clients/basic-ts/.

Installation

Dependencies

Install the required packages:

Package Configuration

Your package.json should include:

Client Implementation

1. Setting Up the Transport

The client uses StdioClientTransport to communicate with the MCP server via standard input/output:

2. Creating the Client

Initialize the MCP client with capabilities:

3. Connecting to the Server

Establish the connection:

Working with MCP Capabilities

Listing Available Resources

Retrieve all available resources from the server:

Reading Resources

Access specific resources by URI:

Working with Prompts

Prompts are reusable message templates:

Calling Tools

Execute server-side tools:

Complete Example

Here’s a complete client implementation from ~/workspace/source/clients/basic-ts/src/index.ts:

Client API Methods

The MCP Client provides these key methods:

Building and Running

Compile the TypeScript code:

Run the client:

Error Handling

Always handle connection and execution errors:

Next Steps