@pkwadsy/grok-mcp 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -13
- package/dist/index.js +57 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,21 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
MCP server that wraps the xAI Grok API. Lets Claude and other AI agents delegate thinking, planning, and real-time search to Grok.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
npm install -g @pkwadsy/grok-mcp
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Or use directly with npx:
|
|
7
|
+
One-liner to add to Claude Code on any device:
|
|
12
8
|
|
|
13
9
|
```bash
|
|
14
|
-
npx @pkwadsy/grok-mcp
|
|
10
|
+
claude mcp add grok -e XAI_API_KEY=your-key -- npx -y @pkwadsy/grok-mcp
|
|
15
11
|
```
|
|
16
12
|
|
|
17
13
|
You need an xAI API key from [console.x.ai](https://console.x.ai).
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
### Alternative: project config
|
|
20
16
|
|
|
21
17
|
Add to your project's `.mcp.json`:
|
|
22
18
|
|
|
@@ -26,7 +22,7 @@ Add to your project's `.mcp.json`:
|
|
|
26
22
|
"grok": {
|
|
27
23
|
"type": "stdio",
|
|
28
24
|
"command": "npx",
|
|
29
|
-
"args": ["@pkwadsy/grok-mcp"],
|
|
25
|
+
"args": ["-y", "@pkwadsy/grok-mcp"],
|
|
30
26
|
"env": {
|
|
31
27
|
"XAI_API_KEY": "your-xai-api-key"
|
|
32
28
|
}
|
|
@@ -44,16 +40,17 @@ Single tool with options for different use cases.
|
|
|
44
40
|
| Parameter | Type | Required | Description |
|
|
45
41
|
|-----------|------|----------|-------------|
|
|
46
42
|
| `prompt` | string | yes | The question or task for Grok |
|
|
43
|
+
| `files` | array | no | Files to include in context (path, optional start_line/end_line) |
|
|
47
44
|
| `system_prompt` | string | no | Custom system prompt |
|
|
48
|
-
| `model` | string | no | Model to use (default: `grok-4.20-
|
|
49
|
-
| `web_search` | boolean | no |
|
|
45
|
+
| `model` | string | no | Model to use (default: `grok-4.20-multi-agent`) |
|
|
46
|
+
| `web_search` | boolean | no | Web search, enabled by default |
|
|
50
47
|
| `x_search` | boolean | no | Enable X/Twitter search |
|
|
51
48
|
|
|
52
49
|
### Available Models
|
|
53
50
|
|
|
54
|
-
- `grok-4.20-
|
|
51
|
+
- `grok-4.20-multi-agent` — multi-agent mode, great for architecture and planning (default)
|
|
52
|
+
- `grok-4.20-reasoning` — flagship reasoning
|
|
55
53
|
- `grok-4.20-non-reasoning` — fast, no reasoning
|
|
56
|
-
- `grok-4.20-multi-agent` — multi-agent mode, great for architecture and planning
|
|
57
54
|
- `grok-4.1-fast-reasoning` — cheaper reasoning
|
|
58
55
|
- `grok-4.1-fast-non-reasoning` — cheapest, fast
|
|
59
56
|
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
-
import {
|
|
4
|
+
import { globSync, readFileSync } from "node:fs";
|
|
5
|
+
import { resolve } from "node:path";
|
|
5
6
|
import { z } from "zod";
|
|
7
|
+
function parseFileArg(arg) {
|
|
8
|
+
// "path/to/file:10-30" or "path/to/file:10" or "path/to/file" or "src/**/*.ts"
|
|
9
|
+
const match = arg.match(/^(.+?):(\d+)(?:-(\d+))?$/);
|
|
10
|
+
let pattern;
|
|
11
|
+
let startLine;
|
|
12
|
+
let endLine;
|
|
13
|
+
if (match) {
|
|
14
|
+
pattern = match[1];
|
|
15
|
+
startLine = parseInt(match[2], 10);
|
|
16
|
+
endLine = match[3] ? parseInt(match[3], 10) : startLine;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
pattern = arg;
|
|
20
|
+
}
|
|
21
|
+
const resolved = resolve(pattern);
|
|
22
|
+
if (/[*?[\]]/.test(pattern)) {
|
|
23
|
+
const paths = globSync(pattern).sort();
|
|
24
|
+
if (paths.length === 0) {
|
|
25
|
+
return [{ path: pattern }]; // will fail at read time with a clear error
|
|
26
|
+
}
|
|
27
|
+
return paths.map((p) => ({ path: resolve(p), startLine, endLine }));
|
|
28
|
+
}
|
|
29
|
+
return [{ path: resolved, startLine, endLine }];
|
|
30
|
+
}
|
|
6
31
|
const apiKey = process.env.XAI_API_KEY;
|
|
7
32
|
if (!apiKey) {
|
|
8
33
|
console.error("XAI_API_KEY environment variable is required");
|
|
@@ -48,13 +73,9 @@ async function callGrok(options) {
|
|
|
48
73
|
server.tool("ask_grok", `Ask Grok a question. Grok is great for thinking, planning, architecture, and real-time search via web and X/Twitter. Use web_search for current information from the internet. Use x_search to find and analyze posts on X/Twitter. IMPORTANT: Grok has no context about your conversation or codebase. Always include all relevant context directly in the prompt — file contents, error messages, architecture details, constraints, and goals. The more context you provide, the better Grok's response will be. Do not assume Grok knows anything about the current project. Use the files parameter to automatically include file contents with line numbers — this is preferred over pasting code into the prompt. File paths are resolved relative to the server working directory: ${process.cwd()}`, {
|
|
49
74
|
prompt: z.string().describe("The question or task for Grok. Include all relevant context — constraints, background, and goals — since Grok has no access to your conversation or files. Use the files parameter to attach source code rather than pasting it inline"),
|
|
50
75
|
files: z
|
|
51
|
-
.array(z.
|
|
52
|
-
path: z.string().describe("Absolute path to the file"),
|
|
53
|
-
start_line: z.number().optional().describe("First line to include (1-based, inclusive)"),
|
|
54
|
-
end_line: z.number().optional().describe("Last line to include (1-based, inclusive)"),
|
|
55
|
-
}))
|
|
76
|
+
.array(z.string())
|
|
56
77
|
.optional()
|
|
57
|
-
.describe(
|
|
78
|
+
.describe('Files to include in context. Compact syntax: "path/to/file" (whole file), "path/to/file:10-30" (lines 10-30), "path/to/file:10" (just line 10), "src/**/*.ts" (glob pattern). Paths resolve relative to server cwd. All files are sent to Grok with line numbers.'),
|
|
58
79
|
system_prompt: z
|
|
59
80
|
.string()
|
|
60
81
|
.optional()
|
|
@@ -81,25 +102,46 @@ server.tool("ask_grok", `Ask Grok a question. Grok is great for thinking, planni
|
|
|
81
102
|
if (files && files.length > 0) {
|
|
82
103
|
const cwd = process.cwd();
|
|
83
104
|
const fileBlocks = [`Working directory: ${cwd}\n`];
|
|
84
|
-
|
|
105
|
+
const errors = [];
|
|
106
|
+
const specs = files.flatMap(parseFileArg);
|
|
107
|
+
for (const spec of specs) {
|
|
85
108
|
try {
|
|
86
|
-
const raw =
|
|
109
|
+
const raw = readFileSync(spec.path, "utf-8");
|
|
87
110
|
const allLines = raw.split("\n");
|
|
88
|
-
const
|
|
89
|
-
|
|
111
|
+
const totalLines = allLines.length;
|
|
112
|
+
if (spec.startLine && spec.startLine > totalLines) {
|
|
113
|
+
errors.push(`${spec.path}: line ${spec.startLine} is past end of file (${totalLines} lines)`);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (spec.endLine && spec.endLine > totalLines) {
|
|
117
|
+
errors.push(`${spec.path}: line ${spec.endLine} is past end of file (${totalLines} lines)`);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (spec.startLine && spec.endLine && spec.startLine > spec.endLine) {
|
|
121
|
+
errors.push(`${spec.path}: start line ${spec.startLine} is after end line ${spec.endLine}`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const start = spec.startLine ? spec.startLine - 1 : 0;
|
|
125
|
+
const end = spec.endLine ? spec.endLine : totalLines;
|
|
90
126
|
const sliced = allLines.slice(start, end);
|
|
91
127
|
const numbered = sliced
|
|
92
128
|
.map((line, i) => `${start + i + 1}\t${line}`)
|
|
93
129
|
.join("\n");
|
|
94
|
-
const range =
|
|
95
|
-
? `:${
|
|
130
|
+
const range = spec.startLine || spec.endLine
|
|
131
|
+
? `:${spec.startLine ?? 1}-${spec.endLine ?? totalLines}`
|
|
96
132
|
: "";
|
|
97
|
-
fileBlocks.push(`--- ${
|
|
133
|
+
fileBlocks.push(`--- ${spec.path}${range} ---\n${numbered}\n---`);
|
|
98
134
|
}
|
|
99
135
|
catch (err) {
|
|
100
|
-
|
|
136
|
+
errors.push(`${spec.path}: ${err instanceof Error ? err.message : String(err)}`);
|
|
101
137
|
}
|
|
102
138
|
}
|
|
139
|
+
if (errors.length > 0) {
|
|
140
|
+
return {
|
|
141
|
+
isError: true,
|
|
142
|
+
content: [{ type: "text", text: `Failed to read ${errors.length} file(s):\n${errors.join("\n")}\n\nFix the paths and try again.` }],
|
|
143
|
+
};
|
|
144
|
+
}
|
|
103
145
|
userContent = `${fileBlocks.join("\n\n")}\n\n${prompt}`;
|
|
104
146
|
}
|
|
105
147
|
messages.push({ role: "user", content: userContent });
|