This repo now includes 2 parts:
- Lite mcp sdk for javascript
- Lite mcp for javascript
The goal of this project is to make best of the AI Agent tech available to any Grandmaπ΅ who learned 5 minutes of javascript.
Because I want the project to condense on the running logics, rather than distracted by the types.
Lite mcp for javascript is the BEST yet way to start a REMOTE SSE javascript MCP server and Client.
REAL COPY AND PASTE experince for you to get started with MCP.
npm install litemcpjs
npm install express
npm install cors
import { MCPClient } from "litemcpjs";
async function main() {
const client = new MCPClient("http://localhost:3000") // change this to your server url
await client.connect()
await client.listTools()
const result = await client.callTool("calculate-bmi", {weightKg: 70, heightM: 1.75})
console.log(result)
}
main();
const { MCPServerInit } = require("litemcpjs");
// define the tools
const TOOLS = [
{
name: "calculate-bmi",
description: "Calculate the BMI of a person",
inputSchema: {
type: "object",
properties: {
weightKg: { type: "number" },
heightM: { type: "number" },
},
required: ["weightKg", "heightM"],
},
method: async ({ weightKg, heightM }) => {
return {
content: [{
type: "text",
text: String(weightKg / (heightM * heightM))
}]
}
}
}
];
// define extra handlers for resources etc.
const handlers = {};
// compatible for server have .use(middleware) method such as express
const middlewares = [
cors(),
];
const MCPapp = express();
const MCPServer = MCPServerInit(
MCPapp,
middlewares,
handlers,
TOOLS,
).listen(5556);
For Anyone wants a little more low level control over the MCP protocol:
Lite mcp sdk for javascript is a lightweight mcp sdk remix with minimal dependencies.
npm install lite-mcp-sdk
const { Server, SSEServerTransport } = require("lite-mcp-sdk");
//or module import
import { Server, SSEServerTransport } from "lite-mcp-sdk";
//create a server
const server = new Server({
port: 3000,
transport: new SSEServerTransport(),
});
// 1. Define the server info
const server = new Server({
name: "example-servers/puppeteer",
version: "0.1.0",
}, {
capabilities: {
resources: {},
tools: {},
},
});
// 2. Define the request handlers
server.setRequestHandler("tools/call", async (request) => {
const { name, arguments: args } = request.params;
return handleToolCall(name, args);
});
//3. Define the transport
const transports = new Map();
const transport = new SSEServerTransport("/messages", res);
transports.set(clientId, transport);
//4. Connect the server to the transport
await server.connect(transport);
//5. Handle the post message
await transport.handlePostMessage(req, res);
import { Client, SSEClientTransport } from "lite-mcp-sdk";
// 1. Define the client
let client = new Client( {
name: "fastmcp-client",
version: "0.1.0"
}, {
capabilities: {
sampling: {},
roots: {
listChanged: true,
},
},
} );
// 2. Init the client transport
const serverUrl = "http://localhost:3000"; // for example
const backendUrl = new URL(`${serverUrl}/sse`);
backendUrl.searchParams.append("transportType", "sse");
backendUrl.searchParams.append("url", `${serverUrl}/sse`);
const headers = {};
const clientTransport = new SSEClientTransport(backendUrl, {
eventSourceInit: {
fetch: (url, init) => fetch(url, { ...init, headers }),
},
requestInit: {
headers,
},
});
// 3. Connect the client to the transport
await client.connect(clientTransport);
const abortController = new AbortController();
let progressTokenRef = 0;
// 4. start the tool call
const name = "toolcall name";
const params = {}; // tool call params
const response = await client.request(
{
method: "tools/call",
params: {
name,
arguments: params,
_meta: {
progressToken: progressTokenRef++,
},
},
},
{signal: abortController.signal,}
);
The project is clearly and cleanlyorganized into the following directories:
src
βββ index.js
βββ lib
βββ client
β βββ client.js
β βββ clientTransport.js
βββ server
β βββ server.js
β βββ serverTransport.js
βββ shared
βββ helpers
β βββ constants.js
β βββ util.js
βββ protocol.js
βββ validators
βββ capabilityValidators.js
βββ schemaValidators.js (IN CONSTRUCTION)
NO More zod types!!! zod types contributes to unneccessary complexity for easy setups.
Furthermore, the way zod type is used in the original sdk is rigid and not flexible, and it is an well received issue.
Focus on SSE, and removed stdio support for simplicity considerations, since it is not a as univerial solution as SSE.
Suprisingly, the SSE solution is scarce on the internet, so I decided to write my own.
BELOW images shows that stdio support is always descripted, but no SSE support is mentioned.
Even when you search deliberately for SSE, result is scarce, SEE for first page google results.
Replace codes like requestSchema.parse to fully customizable validators and hand it over to the user to define the validation logic here. e.g.
const validatedMessage = validator(message);
How timeout is used in original sdk is vague and has some redundant implementations,which are removed in lite mcp for not causing further confusion.(Ok I think they also removed these in latest version)
They actually listed this problem in the official roadmap. lol
Lite MCP SDK provides better documentation on how to use and modify the sdk, with more ready to copy code examples.
Lite MCP SDK:
src
βββ index.js
βββ lib
βββ client
β βββ client.js
β βββ clientTransport.js
βββ server
β βββ server.js
β βββ serverTransport.js
βββ shared
βββ helpers
β βββ constants.js
β βββ util.js
βββ protocol.js
βββ validators
βββ capabilityValidators.js
βββ schemaValidators.js (IN CONSTRUCTION)
Official sdk:
Although it is listed on the roadmap, I think putting auth in this project is poor product design.
Honestly I think left users to handle auth problem provides more flexibility and better dev experience.
src/
βββ client
β βββ auth.test.ts
β βββ auth.ts
β βββ cross-spawn.test.ts
β βββ index.test.ts
β βββ index.ts
β βββ sse.test.ts
β βββ sse.ts
β βββ stdio.test.ts
β βββ stdio.ts
β βββ websocket.ts
βββ cli.ts
βββ inMemory.test.ts
βββ inMemory.ts
βββ integration-tests
β βββ process-cleanup.test.ts
βββ server
β βββ auth
β β βββ clients.ts
β β βββ errors.ts
β β βββ handlers
β β β βββ authorize.test.ts
β β β βββ authorize.ts
β β β βββ metadata.test.ts
β β β βββ metadata.ts
β β β βββ register.test.ts
β β β βββ register.ts
β β β βββ revoke.test.ts
β β β βββ revoke.ts
β β β βββ token.test.ts
β β β βββ token.ts
β β βββ middleware
β β β βββ allowedMethods.test.ts
β β β βββ allowedMethods.ts
β β β βββ bearerAuth.test.ts
β β β βββ bearerAuth.ts
β β β βββ clientAuth.test.ts
β β β βββ clientAuth.ts
β β βββ provider.ts
β β βββ router.test.ts
β β βββ router.ts
β β βββ types.ts
β βββ completable.test.ts
β βββ completable.ts
β βββ index.test.ts
β βββ index.ts
β βββ mcp.test.ts
β βββ mcp.ts
β βββ sse.ts
β βββ stdio.test.ts
β βββ stdio.ts
βββ shared
β βββ auth.ts
β βββ protocol.test.ts
β βββ protocol.ts
β βββ stdio.test.ts
β βββ stdio.ts
β βββ transport.ts
β βββ uriTemplate.test.ts
β βββ uriTemplate.ts
βββ types.ts
Feature | mcp-official-sdk | lite-mcp-sdk |
---|---|---|
Validation | zod | Validators |
Transport | stdio or SSE | SSE only |
Language | typescript | javascript |
Auth | included | optional |
Documentation | vague | clear |
Architecture | complex | lightweight |
- Custom Validators (under construction)
- Add dashboard for monitoring and managing the MCP server and client
- Add ts types
- Add python sdk
Contributions are welcome! Please open an issue or submit a pull request.
Please read the contributing document.
This project is licensed under the MIT License. See the LICENSE file for details.
Inspired by Original anthropic sdk,which license is also included here.
If you have any questions or suggestions, please leave a comment here. Or contact me via email: borui_cai@qq.com