|
1 | 1 | # mcp-typescript
|
2 |
| -TypeScript implementation of the Model Context Protocol |
| 2 | + |
| 3 | +TypeScript implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This TypeScript SDK implements the full MCP specification, making it easy to: |
| 8 | + |
| 9 | +- Build MCP clients that can connect to any MCP server |
| 10 | +- Create MCP servers that expose resources, prompts and tools |
| 11 | +- Use standard transports like stdio, SSE, and WebSocket |
| 12 | +- Handle all MCP protocol messages and lifecycle events |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @modelcontextprotocol/sdk |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### Creating a Client |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { Client } from "@modelcontextprotocol/sdk/client"; |
| 26 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio"; |
| 27 | + |
| 28 | +const transport = new StdioClientTransport({ |
| 29 | + command: "path/to/server", |
| 30 | +}); |
| 31 | + |
| 32 | +const client = new Client({ |
| 33 | + name: "example-client", |
| 34 | + version: "1.0.0", |
| 35 | +}); |
| 36 | + |
| 37 | +await client.connect(transport); |
| 38 | + |
| 39 | +// List available resources |
| 40 | +const resources = await client.request( |
| 41 | + { method: "resources/list" }, |
| 42 | + ListResourcesResultSchema |
| 43 | +); |
| 44 | + |
| 45 | +// Read a specific resource |
| 46 | +const resourceContent = await client.request( |
| 47 | + { |
| 48 | + method: "resources/read", |
| 49 | + params: { |
| 50 | + uri: "file:///example.txt" |
| 51 | + } |
| 52 | + }, |
| 53 | + ReadResourceResultSchema |
| 54 | +); |
| 55 | +``` |
| 56 | + |
| 57 | +### Creating a Server |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { Server } from "@modelcontextprotocol/sdk/server"; |
| 61 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio"; |
| 62 | + |
| 63 | +const server = new Server({ |
| 64 | + name: "example-server", |
| 65 | + version: "1.0.0", |
| 66 | +}); |
| 67 | + |
| 68 | +server.setRequestHandler(ListResourcesRequestSchema, async () => { |
| 69 | + return { |
| 70 | + resources: [ |
| 71 | + { |
| 72 | + uri: "file:///example.txt", |
| 73 | + name: "Example Resource", |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | +}); |
| 78 | + |
| 79 | +server.setRequestHandler(ReadResourceRequestSchema, async (request) => { |
| 80 | + if (request.params.uri === "file:///example.txt") { |
| 81 | + return { |
| 82 | + contents: [ |
| 83 | + { |
| 84 | + uri: "file:///example.txt", |
| 85 | + mimeType: "text/plain", |
| 86 | + text: "This is the content of the example resource.", |
| 87 | + }, |
| 88 | + ], |
| 89 | + }; |
| 90 | + } else { |
| 91 | + throw new Error("Resource not found"); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +const transport = new StdioServerTransport(); |
| 96 | +await server.connect(transport); |
| 97 | +``` |
| 98 | + |
| 99 | +## Documentation |
| 100 | + |
| 101 | +- [MCP Specification](https://modelcontextprotocol.github.io) |
| 102 | +- [Example Servers](https://github.com/modelcontextprotocol/example-servers) |
| 103 | + |
| 104 | +## Contributing |
| 105 | + |
| 106 | +Issues and pull requests are welcome on GitHub at https://github.com/modelcontextprotocol/typescript-sdk. |
| 107 | + |
| 108 | +## License |
| 109 | + |
| 110 | +This project is licensed under the MIT License—see the [LICENSE](LICENSE) file for details. |
0 commit comments