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:| Method | Description | Returns |
|---|---|---|
connect(transport) | Establishes connection to MCP server | Promise<void> |
listPrompts() | Lists all available prompts | Promise<PromptsListResult> |
getPrompt(params) | Retrieves a prompt with arguments | Promise<PromptResult> |
listResources() | Lists all static resources | Promise<ResourcesListResult> |
listResourceTemplates() | Lists dynamic resource templates | Promise<ResourceTemplatesListResult> |
readResource(params) | Reads a specific resource | Promise<ResourceResult> |
listTools() | Lists all available tools | Promise<ToolsListResult> |
callTool(params) | Executes a tool with arguments | Promise<ToolResult> |
close() | Closes the connection | Promise<void> |
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