-
Notifications
You must be signed in to change notification settings - Fork 280
Add Tutorial for Creating an MCP Client in Typescript #147
Add Tutorial for Creating an MCP Client in Typescript #147
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just got around reviewing this. Thank you so much for taking care of this. this is a great improvement.
- Mac or Windows computer | ||
- Node.js 16 or higher installed | ||
- Latest version of `npm` and `npx` installed | ||
- Anthropic API key (Claude) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably clarify that others can be used, but this tutorial uses Claude.
import readline from "readline/promises"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add a comment that these specific bits are for?
async connectToServer(serverScriptPath: string) { | ||
try { | ||
const isJs = serverScriptPath.endsWith(".js"); | ||
const isPy = serverScriptPath.endsWith(".py"); | ||
if (!isJs && !isPy) { | ||
throw new Error("Server script must be a .js or .py file"); | ||
} | ||
const command = isPy | ||
? process.platform === "win32" | ||
? "python" | ||
: "python3" | ||
: process.execPath; | ||
|
||
this.transport = new StdioClientTransport({ | ||
command, | ||
args: [serverScriptPath], | ||
}); | ||
this.mcp.connect(this.transport); | ||
|
||
const toolsResult = await this.mcp.listTools(); | ||
this.tools = toolsResult.tools.map((tool) => { | ||
return { | ||
name: tool.name, | ||
description: tool.description, | ||
input_schema: tool.inputSchema, | ||
}; | ||
}); | ||
console.log( | ||
"Connected to server with tools:", | ||
this.tools.map(({ name }) => name) | ||
); | ||
} catch (e) { | ||
console.log("Failed to connect to MCP server: ", e); | ||
throw e; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specialcasing .js or .py might give the impression that this is the standard behaviour. I would prefer if we assume we read the configs from a json or yaml file.
…otocol#147) * Add mcp-client-typescript quickstart guide * Improve client running explanation, best practices, etc. * Set new python mcp client github link * Only call list tools at initialization * Add package.json and tsconfig.json setup section --------- Co-authored-by: David Soria Parra <167242713+dsp-ant@users.noreply.github.com>
This PR relies on the
quickstart-resources
PR merging first as it links to the Python and Typescript MCP clients.Specifically, this change will add a section to the Quickstart For Client Developers focused on Node/Typescript
Motivation and Context
MCP Client tutorials exist for Python and Java but not Node. Including this section improves the DX for MCP Client developers.
How Has This Been Tested?
The code in this tutorial was validated via connecting to multiple MCP Servers locally.
Breaking Changes
None
Types of changes
Checklist
Additional context
Fixes #103