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
Yourpackage.json should include:
Client Implementation
1. Setting Up the Transport
The client usesStdioClientTransport 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
- Learn about integrating with Ollama
- Explore the Python client implementation
- Build custom tools for your MCP server