Quick and Dirty Typescript bootstrapping
?
R
BashEasily bootstrap a TypeScript environment
1OPTION 1
2===
3npm i -g typescript && mkdir typescript && cd typescript && tsc --init
4cat tsconfig.json
5touch typescript.ts && tsc -w
6
7OPTION 2
8===
9# Create project directory
10mkdir mcp-client-typescript
11cd mcp-client-typescript
12
13# Initialize npm project
14npm init -y
15
16# Install dependencies
17npm install @anthropic-ai/sdk @modelcontextprotocol/sdk dotenv
18
19# Install dev dependencies
20npm install -D @types/node typescript
21
22# Create source file
23touch index.ts
24
25# Update your package.json to set type: "module" and a build script:
26{
27 "type": "module",
28 "scripts": {
29 "build": "tsc && chmod 755 build/index.js"
30 }
31}
32
33# Create a tsconfig.json in the root of your project:
34{
35 "compilerOptions": {
36 "target": "ES2022",
37 "module": "Node16",
38 "moduleResolution": "Node16",
39 "outDir": "./build",
40 "rootDir": "./",
41 "strict": true,
42 "esModuleInterop": true,
43 "skipLibCheck": true,
44 "forceConsistentCasingInFileNames": true
45 },
46 "include": ["index.ts"],
47 "exclude": ["node_modules"]
48}
49
50# Create a .env file to store it:
51echo "ANTHROPIC_API_KEY=<your key here>" > .env
52
53# Add .env to your .gitignore:
54echo ".env" >> .gitignore
55
56# First, let’s set up our imports and create the basic client class in index.ts:
57import { Anthropic } from "@anthropic-ai/sdk";
58import {
59 MessageParam,
60 Tool,
61} from "@anthropic-ai/sdk/resources/messages/messages.mjs";
62import { Client } from "@modelcontextprotocol/sdk/client/index.js";
63import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
64import readline from "readline/promises";
65import dotenv from "dotenv";
66
67dotenv.config();
68
69const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
70if (!ANTHROPIC_API_KEY) {
71 throw new Error("ANTHROPIC_API_KEY is not set");
72}
73
74class MCPClient {
75 private mcp: Client;
76 private anthropic: Anthropic;
77 private transport: StdioClientTransport | null = null;
78 private tools: Tool[] = [];
79
80 constructor() {
81 this.anthropic = new Anthropic({
82 apiKey: ANTHROPIC_API_KEY,
83 });
84 this.mcp = new Client({ name: "mcp-client-cli", version: "1.0.0" });
85 }
86 // methods will go here
87}
88
89
90
91
92
93
94
95
96Created on 4/12/2018