Skip to content

Commit 3afb790

Browse files
author
hideya
committed
Add support for xAI Grok
1 parent 1c85f2e commit 3afb790

File tree

7 files changed

+131
-45
lines changed

7 files changed

+131
-45
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ ANTHROPIC_API_KEY=sk-ant-...
44
OPENAI_API_KEY=sk-proj-...
55
# https://aistudio.google.com/apikey
66
GOOGLE_API_KEY=AI...
7+
# https://console.x.ai
8+
XAI_API_KEY=xai-...
79

810
# BRAVE_API_KEY=BSA...
911
# GITHUB_PERSONAL_ACCESS_TOKEN=github_pat_...

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [Unreleased]
9+
10+
### Added
11+
- Support for xAI Grok
12+
13+
814
## [0.3.1] - 2025-07-05
15+
16+
### Changed
917
- Update dependencies
1018
- Refine REAMDE.md
1119

README.md

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# MCP to LangChain Tools Conversion Utility / TypeScript [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/@h1deya/langchain-mcp-tools.svg)](https://www.npmjs.com/package/@h1deya/langchain-mcp-tools) [![network dependents](https://dependents.info/hideya/langchain-mcp-tools-ts/badge)](https://dependents.info/hideya/langchain-mcp-tools-ts)
22

3-
A simple, lightweight library intended to simplify the use of
3+
A simple, lightweight library to use
44
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
5-
server tools with LangChain.
5+
server tools from LangChain.
6+
7+
<img style="width:450px;" alt="langchain-mcp-tools-diagram" src="https://raw.githubusercontent.com/hideya/langchain-mcp-tools-py/refs/heads/main/docs/images/langchain-mcp-tools-diagram.png" />
68

79
Its simplicity and extra features, such as
810
[tools schema adjustments for LLM compatibility](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README.md#llm-provider-schema-compatibility)
@@ -14,30 +16,6 @@ However, it only supports text results of tool calls and does not support MCP fe
1416
which supports comprehensive integration with LangChain, has been released.
1517
You may want to consider using it if you don't have specific needs for this library.
1618

17-
## Introduction
18-
19-
This package is intended to simplify the use of
20-
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
21-
server tools with LangChain / TypeScript.
22-
23-
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/) is the de facto industry standard
24-
that dramatically expands the scope of LLMs by enabling the integration of external tools and resources,
25-
including DBs, Cloud Storages, GitHub, Docker, Slack, and more.
26-
There are quite a few useful MCP servers already available.
27-
See [MCP Server Listing on the Official Site](https://github.com/modelcontextprotocol/servers?tab=readme-ov-file#model-context-protocol-servers).
28-
29-
This utility's goal is to make these numerous MCP servers easily accessible from LangChain.
30-
It contains a utility function `convertMcpToLangchainTools()`.
31-
This async function handles parallel initialization of specified multiple MCP servers
32-
and converts their available tools into an array of LangChain-compatible tools.
33-
It also performs LLM provider-specific schema transformations
34-
to prevent [schema compatibility issues](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README.md#llm-provider-schema-compatibility)
35-
36-
For detailed information on how to use this library, please refer to the following document:
37-
["Supercharging LangChain: Integrating 2000+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a).
38-
A Python equivalent of this utility is available
39-
[here](https://pypi.org/project/langchain-mcp-tools)
40-
4119
## Prerequisites
4220

4321
- Node.js 18+
@@ -48,15 +26,8 @@ A Python equivalent of this utility is available
4826
npm i @h1deya/langchain-mcp-tools
4927
```
5028

51-
## API docs
52-
53-
Can be found [here](https://hideya.github.io/langchain-mcp-tools-ts/modules.html)
54-
5529
## Quick Start
5630

57-
A minimal but complete working usage example can be found
58-
[in this example in the langchain-mcp-tools-ts-usage repo](https://github.com/hideya/langchain-mcp-tools-ts-usage/blob/main/src/index.ts)
59-
6031
`convertMcpToLangchainTools()` utility function accepts MCP server configurations
6132
that follow the same structure as
6233
[Claude for Desktop](https://modelcontextprotocol.io/quickstart/user),
@@ -125,9 +96,30 @@ A minimal but complete working usage example can be found
12596
For hands-on experimentation with MCP server integration,
12697
try [this MCP Client CLI tool built with this library](https://www.npmjs.com/package/@h1deya/mcp-client-cli)
12798

128-
## Building from Source
99+
A Python equivalent of this utility is available
100+
[here](https://pypi.org/project/langchain-mcp-tools)
129101

130-
See [README_DEV.md](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README_DEV.md) for details.
102+
## Introduction
103+
104+
This package is intended to simplify the use of
105+
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
106+
server tools with LangChain / TypeScript.
107+
108+
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/) is the de facto industry standard
109+
that dramatically expands the scope of LLMs by enabling the integration of external tools and resources,
110+
including DBs, Cloud Storages, GitHub, Docker, Slack, and more.
111+
There are quite a few useful MCP servers already available.
112+
See [MCP Server Listing on the Official Site](https://github.com/modelcontextprotocol/servers?tab=readme-ov-file#model-context-protocol-servers).
113+
114+
This utility's goal is to make these numerous MCP servers easily accessible from LangChain.
115+
It contains a utility function `convertMcpToLangchainTools()`.
116+
This async function handles parallel initialization of specified multiple MCP servers
117+
and converts their available tools into an array of LangChain-compatible tools.
118+
It also performs LLM provider-specific schema transformations
119+
to prevent [schema compatibility issues](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README.md#llm-provider-schema-compatibility)
120+
121+
For detailed information on how to use this library, please refer to the following document:
122+
["Supercharging LangChain: Integrating 2000+ MCP with ReAct"](https://medium.com/@h1deya/supercharging-langchain-integrating-450-mcp-with-react-d4e467cbf41a).
131123

132124
## MCP Protocol Support
133125

@@ -141,12 +133,19 @@ The library uses LangChain's `response_format: 'content'` (the default), which o
141133
While MCP tools can return multiple content types (text, images, etc.), this library currently filters and uses only text content.
142134
- **MCP Features**: Only MCP [Tools](https://modelcontextprotocol.io/docs/concepts/tools) are supported. Other MCP features like Resources, Prompts, and Sampling are not implemented.
143135

144-
### Notes:
136+
### Notes
145137

146138
- **LLM Compatibility and Schema Transformations**: The library can perform schema transformations for LLM compatibility.
147139
[See below](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README.md#llm-provider-schema-compatibility) for details.
148140
- **Passing PATH Env Variable**: The library automatically adds the `PATH` environment variable to stdio server configrations if not explicitly provided to ensure servers can find required executables.
149141

142+
## API docs
143+
144+
Can be found [here](https://hideya.github.io/langchain-mcp-tools-ts/modules.html)
145+
146+
## Building from Source
147+
148+
See [README_DEV.md](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README_DEV.md) for details.
150149

151150
## Features
152151

@@ -310,15 +309,25 @@ Can be found [here](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/C
310309
3. **Test explicit transports**: Try forcing specific transport types to isolate auto-detection issues
311310
4. **Verify server independently**: Refer to [Debugging Section in MCP documentation](https://modelcontextprotocol.io/docs/tools/debugging)
312311

312+
### Debugging Authentication
313+
314+
1. **Check your tokens/credentials** - Most auth failures are due to expired or incorrect tokens
315+
2. **Verify token permissions** - Some MCP servers require specific scopes (e.g., GitHub Copilot license)
316+
3. **Test with curl** - Try a simple HTTP request to verify your auth setup:
317+
318+
```bash
319+
curl -H "Authorization: Bearer your-token" https://api.example.com/mcp/
320+
```
321+
313322
### LLM Provider Schema Compatibility
314323

315324
Different LLM providers have incompatible JSON Schema requirements for function calling:
316325

317326
- **OpenAI requires**: Optional fields must be nullable (`.optional()` + `.nullable()`)
318327
for function calling (based on Structured Outputs API requirements,
319-
strict enforcement coming in future SDK versions)"
328+
strict enforcement coming in future SDK versions)
320329
- **Google Gemini API**: Rejects nullable fields and `$defs` references, requires strict OpenAPI 3.0 subset compliance
321-
- **Anthropic Claude**: Very relaxed schema requirements with no documented restrictions
330+
- **Anthropic Claude** and **xAI Grok**: Very relaxed schema requirements with no documented restrictions
322331

323332
**Note**: Google Vertex AI provides OpenAI-compatible endpoints that support nullable fields.
324333

@@ -365,8 +374,8 @@ const { tools, cleanup } = await convertMcpToLangchainTools(
365374
| Provider | Transformations Applied |
366375
|----------|------------------------|
367376
| `openai` | Makes optional fields nullable, handles union types |
368-
| `google_gemini` | Filters invalid required fields, fixes anyOf variants, removes unsupported features |
369-
| `anthropic` | Accepts schemas as-is, but handles them efficiently |
377+
| `google_gemini` or `google_genai` | Filters invalid required fields, fixes anyOf variants, removes unsupported features |
378+
| `anthropic` and `xai` | Accepts schemas as-is, but handles them efficiently |
370379

371380
For other providers, try without specifying the option:
372381

@@ -432,5 +441,5 @@ Available log levels: `"fatal" | "error" | "warn" | "info" | "debug" | "trace"`
432441

433442
### For Developers
434443

435-
See [README_DEV.md](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README.md)
444+
See [README_DEV.md](https://github.com/hideya/langchain-mcp-tools-ts/blob/main/README_DEV.md)
436445
for more information about development and testing.

package-lock.json

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"@langchain/google-genai": "^0.2.12",
7979
"@langchain/langgraph": "^0.2.36",
8080
"@langchain/openai": "^0.3.16",
81+
"@langchain/xai": "^0.1.0",
8182
"@types/node": "^22.10.5",
8283
"@types/ws": "^8.18.1",
8384
"@typescript-eslint/eslint-plugin": "^8.19.0",

src/langchain-mcp-tools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface LogOptions {
135135
*
136136
* @public
137137
*/
138-
export type LlmProvider = "openai" | "google_gemini" | "google_genai" | "anthropic" | "none";
138+
export type LlmProvider = "openai" | "google_gemini" | "google_genai" | "anthropic" | "xai" | "none";
139139

140140
/**
141141
* Configuration options for converting MCP servers to LangChain tools.
@@ -237,7 +237,7 @@ function processSchemaForLlmProvider(
237237
}
238238
processedSchema = result.schema;
239239

240-
} else if (llmProvider === "anthropic") {
240+
} else if (llmProvider === "anthropic" || llmProvider === "xai") {
241241
// Anthropic Claude has very relaxed schema requirements with no documented restrictions
242242
// No schema modifications needed
243243
// Claude is tested to work fine with passing the JSON schema directly

testfiles/simple-usage.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { HumanMessage } from "@langchain/core/messages";
44
import { ChatAnthropic } from "@langchain/anthropic";
55
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
66
import { ChatOpenAI } from "@langchain/openai";
7+
import { ChatXAI } from "@langchain/xai";
78
import WebSocket from 'ws';
89
import * as fs from "fs";
910

@@ -199,14 +200,22 @@ export async function test(): Promise<void> {
199200
// model: "gemini-2.5-pro"
200201
});
201202

203+
// const llm = new ChatXAI({
204+
// // https://console.x.ai
205+
// model: "grok-3-mini"
206+
// // model: "grok-4"
207+
// });
208+
202209
let llmProvider: LlmProvider = "none";
203210
if (llm instanceof ChatAnthropic) {
204211
llmProvider = "anthropic";
205212
} else if (llm as object instanceof ChatOpenAI) {
206213
llmProvider = "openai";
207214
} else if (llm as object instanceof ChatGoogleGenerativeAI) {
208215
llmProvider = "google_genai";
209-
}
216+
} else if (llm as object instanceof ChatXAI) {
217+
llmProvider = "xai";
218+
}
210219

211220
const { tools, cleanup } = await convertMcpToLangchainTools(
212221
mcpServers, { llmProvider }

0 commit comments

Comments
 (0)