search1api-mcp 0.1.8 → 0.3.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 +158 -76
- package/build/api.js +13 -2
- package/build/config.js +31 -4
- package/build/http.js +124 -0
- package/build/index.js +24 -23
- package/build/server.js +7 -37
- package/build/tools/crawl.js +2 -2
- package/build/tools/handlers.js +8 -7
- package/build/tools/news.js +2 -2
- package/build/tools/reasoning.js +2 -7
- package/build/tools/search.js +2 -2
- package/build/tools/sitemap.js +2 -2
- package/build/tools/trending.js +2 -2
- package/package.json +9 -6
package/README.md
CHANGED
|
@@ -2,79 +2,102 @@
|
|
|
2
2
|
|
|
3
3
|
[中文文档](./README_zh.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The official MCP server for [Search1API](https://www.search1api.com/?utm_source=mcp) — web search, news, crawling, and more in one API.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Get Your API Key
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
1. Register at [Search1API](https://www.search1api.com/?utm_source=mcp)
|
|
10
|
+
2. Get your API key from the dashboard
|
|
10
11
|
|
|
11
|
-
##
|
|
12
|
+
## Quick Start (Remote MCP)
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
- News search functionality
|
|
15
|
-
- Web page content extraction
|
|
16
|
-
- Website sitemap extraction
|
|
17
|
-
- Deep thinking and complex problem solving with DeepSeek R1
|
|
18
|
-
- Seamless integration with Claude Desktop, Cursor, Windsurf, Cline and other MCP clients
|
|
14
|
+
No installation required. Just configure your MCP client with the remote URL and your API key.
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
### Authentication
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
###
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
18
|
+
Two methods are supported — use whichever your client supports:
|
|
19
|
+
|
|
20
|
+
| Method | Format |
|
|
21
|
+
|--------|--------|
|
|
22
|
+
| Authorization Header | `Authorization: Bearer YOUR_SEARCH1API_KEY` |
|
|
23
|
+
| URL Query Parameter | `https://mcp.search1api.com/mcp?apiKey=YOUR_SEARCH1API_KEY` |
|
|
24
|
+
|
|
25
|
+
### Claude Desktop
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"mcpServers": {
|
|
30
|
+
"search1api": {
|
|
31
|
+
"url": "https://mcp.search1api.com/mcp",
|
|
32
|
+
"headers": {
|
|
33
|
+
"Authorization": "Bearer YOUR_SEARCH1API_KEY"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Claude.ai (Web)
|
|
41
|
+
|
|
42
|
+
Settings > Connectors > Add custom connector:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
https://mcp.search1api.com/mcp?apiKey=YOUR_SEARCH1API_KEY
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Cursor
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"mcpServers": {
|
|
53
|
+
"search1api": {
|
|
54
|
+
"url": "https://mcp.search1api.com/mcp",
|
|
55
|
+
"headers": {
|
|
56
|
+
"Authorization": "Bearer YOUR_SEARCH1API_KEY"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### VS Code
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"servers": {
|
|
68
|
+
"search1api": {
|
|
69
|
+
"type": "http",
|
|
70
|
+
"url": "https://mcp.search1api.com/mcp",
|
|
71
|
+
"headers": {
|
|
72
|
+
"Authorization": "Bearer YOUR_SEARCH1API_KEY"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Claude Code
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
claude mcp add --transport http search1api https://mcp.search1api.com/mcp \
|
|
83
|
+
--header "Authorization: Bearer YOUR_SEARCH1API_KEY"
|
|
84
|
+
```
|
|
76
85
|
|
|
77
|
-
###
|
|
86
|
+
### Windsurf
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"mcpServers": {
|
|
91
|
+
"search1api": {
|
|
92
|
+
"serverUrl": "https://mcp.search1api.com/mcp?apiKey=YOUR_SEARCH1API_KEY"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Local Mode (stdio)
|
|
99
|
+
|
|
100
|
+
If you prefer to run the server locally, use npx — no cloning required:
|
|
78
101
|
|
|
79
102
|
```json
|
|
80
103
|
{
|
|
@@ -90,18 +113,77 @@ More discussions and updates, please follow our official [X](https://x.com/searc
|
|
|
90
113
|
}
|
|
91
114
|
```
|
|
92
115
|
|
|
116
|
+
## Tools
|
|
117
|
+
|
|
118
|
+
### search
|
|
119
|
+
Search the web using Search1API.
|
|
120
|
+
|
|
121
|
+
| Parameter | Required | Default | Description |
|
|
122
|
+
|-----------|----------|---------|-------------|
|
|
123
|
+
| `query` | Yes | - | Search query |
|
|
124
|
+
| `max_results` | No | 10 | Number of results |
|
|
125
|
+
| `search_service` | No | google | google, bing, duckduckgo, yahoo, x, reddit, github, youtube, arxiv, wechat, bilibili, imdb, wikipedia |
|
|
126
|
+
| `crawl_results` | No | 0 | Number of results to crawl for full content |
|
|
127
|
+
| `include_sites` | No | [] | Sites to include |
|
|
128
|
+
| `exclude_sites` | No | [] | Sites to exclude |
|
|
129
|
+
| `time_range` | No | - | day, month, year |
|
|
130
|
+
|
|
131
|
+
### news
|
|
132
|
+
Search for news articles.
|
|
133
|
+
|
|
134
|
+
| Parameter | Required | Default | Description |
|
|
135
|
+
|-----------|----------|---------|-------------|
|
|
136
|
+
| `query` | Yes | - | Search query |
|
|
137
|
+
| `max_results` | No | 10 | Number of results |
|
|
138
|
+
| `search_service` | No | bing | google, bing, duckduckgo, yahoo, hackernews |
|
|
139
|
+
| `crawl_results` | No | 0 | Number of results to crawl for full content |
|
|
140
|
+
| `include_sites` | No | [] | Sites to include |
|
|
141
|
+
| `exclude_sites` | No | [] | Sites to exclude |
|
|
142
|
+
| `time_range` | No | - | day, month, year |
|
|
143
|
+
|
|
144
|
+
### crawl
|
|
145
|
+
Extract content from a URL.
|
|
146
|
+
|
|
147
|
+
| Parameter | Required | Description |
|
|
148
|
+
|-----------|----------|-------------|
|
|
149
|
+
| `url` | Yes | URL to crawl |
|
|
150
|
+
|
|
151
|
+
### sitemap
|
|
152
|
+
Get all related links from a URL.
|
|
153
|
+
|
|
154
|
+
| Parameter | Required | Description |
|
|
155
|
+
|-----------|----------|-------------|
|
|
156
|
+
| `url` | Yes | URL to get sitemap |
|
|
157
|
+
|
|
158
|
+
### reasoning
|
|
159
|
+
Deep thinking and complex problem solving with DeepSeek R1.
|
|
160
|
+
|
|
161
|
+
| Parameter | Required | Description |
|
|
162
|
+
|-----------|----------|-------------|
|
|
163
|
+
| `content` | Yes | The question or problem |
|
|
164
|
+
|
|
165
|
+
### trending
|
|
166
|
+
Get trending topics from popular platforms.
|
|
167
|
+
|
|
168
|
+
| Parameter | Required | Default | Description |
|
|
169
|
+
|-----------|----------|---------|-------------|
|
|
170
|
+
| `search_service` | Yes | - | github, hackernews |
|
|
171
|
+
| `max_results` | No | 10 | Number of items |
|
|
172
|
+
|
|
93
173
|
## Version History
|
|
94
174
|
|
|
95
|
-
- v0.
|
|
96
|
-
- v0.
|
|
97
|
-
- v0.1.
|
|
98
|
-
- v0.1.
|
|
99
|
-
- v0.1.
|
|
100
|
-
- v0.1.
|
|
101
|
-
- v0.1.
|
|
102
|
-
- v0.1.
|
|
103
|
-
- v0.1.
|
|
175
|
+
- v0.3.0: Remote MCP support via Streamable HTTP; per-session API key authentication
|
|
176
|
+
- v0.2.0: Fallback `.env` support for LibreChat integration
|
|
177
|
+
- v0.1.8: X (Twitter) and Reddit search services
|
|
178
|
+
- v0.1.7: Trending tool for GitHub and Hacker News
|
|
179
|
+
- v0.1.6: Wikipedia search service
|
|
180
|
+
- v0.1.5: New search parameters and services (arxiv, wechat, bilibili, imdb)
|
|
181
|
+
- v0.1.4: Reasoning tool with DeepSeek R1
|
|
182
|
+
- v0.1.3: News search
|
|
183
|
+
- v0.1.2: Sitemap
|
|
184
|
+
- v0.1.1: Web crawling
|
|
185
|
+
- v0.1.0: Initial release
|
|
104
186
|
|
|
105
187
|
## License
|
|
106
188
|
|
|
107
|
-
|
|
189
|
+
MIT
|
package/build/api.js
CHANGED
|
@@ -4,24 +4,35 @@ import { log } from './utils.js';
|
|
|
4
4
|
* Unified API request function
|
|
5
5
|
* @param endpoint API endpoint
|
|
6
6
|
* @param data Request data
|
|
7
|
+
* @param apiKey Optional per-session API key (falls back to global API_KEY)
|
|
7
8
|
* @returns API response
|
|
8
9
|
*/
|
|
9
|
-
export async function makeRequest(endpoint, data) {
|
|
10
|
+
export async function makeRequest(endpoint, data, apiKey) {
|
|
11
|
+
const key = apiKey || API_KEY;
|
|
12
|
+
if (!key) {
|
|
13
|
+
throw new Error("API key is required. Provide SEARCH1API_KEY or pass apiKey per-session.");
|
|
14
|
+
}
|
|
10
15
|
const startTime = Date.now();
|
|
11
16
|
const baseUrl = API_CONFIG.BASE_URL.replace(/\/+$/, '');
|
|
12
17
|
const path = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
|
13
18
|
const url = `${baseUrl}${path}`;
|
|
19
|
+
// Debug log for request details (excluding sensitive info)
|
|
20
|
+
log(`Making request to: ${url}`);
|
|
21
|
+
log(`Request data: ${JSON.stringify(data, null, 2)}`);
|
|
14
22
|
try {
|
|
15
23
|
const response = await fetch(url, {
|
|
16
24
|
method: 'POST',
|
|
17
25
|
headers: {
|
|
18
|
-
'Authorization': `Bearer ${
|
|
26
|
+
'Authorization': `Bearer ${key}`,
|
|
19
27
|
'Content-Type': 'application/json',
|
|
20
28
|
},
|
|
21
29
|
body: JSON.stringify(data)
|
|
22
30
|
});
|
|
31
|
+
// Log response status only
|
|
32
|
+
log(`Response status: ${response.status}`);
|
|
23
33
|
if (!response.ok) {
|
|
24
34
|
const errorData = await response.json().catch(() => ({}));
|
|
35
|
+
log(`Error response: ${JSON.stringify(errorData, null, 2)}`);
|
|
25
36
|
throw new Error(`API error: ${response.status} ${errorData.message || response.statusText}`);
|
|
26
37
|
}
|
|
27
38
|
const result = await response.json();
|
package/build/config.js
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { dirname, join } from 'path';
|
|
4
|
+
import { log } from './utils.js';
|
|
5
|
+
// Check API key directly from environment variables first
|
|
6
|
+
let API_KEY = process.env.SEARCH1API_KEY;
|
|
7
|
+
log(`Attempting to read SEARCH1API_KEY from environment: ${API_KEY ? 'Found' : 'Not found'}`);
|
|
8
|
+
// If not found in env, try loading from .env file in project root as fallback
|
|
5
9
|
if (!API_KEY) {
|
|
6
|
-
|
|
10
|
+
log('SEARCH1API_KEY not found in environment, attempting to load from .env file in project root...');
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = dirname(__filename);
|
|
13
|
+
// Path relative to the build directory (build/config.js) to reach project root
|
|
14
|
+
const projectRootEnvPath = join(__dirname, '../.env');
|
|
15
|
+
log(`Attempting to load .env from: ${projectRootEnvPath}`);
|
|
16
|
+
const result = dotenv.config({ path: projectRootEnvPath });
|
|
17
|
+
if (result.error) {
|
|
18
|
+
log(`Failed to load .env from ${projectRootEnvPath}: ${result.error.message}`);
|
|
19
|
+
}
|
|
20
|
+
else if (result.parsed && result.parsed.SEARCH1API_KEY) {
|
|
21
|
+
API_KEY = result.parsed.SEARCH1API_KEY;
|
|
22
|
+
log(`Successfully loaded SEARCH1API_KEY from ${projectRootEnvPath}`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
log(`.env file found at ${projectRootEnvPath}, but SEARCH1API_KEY was not inside.`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// Final check
|
|
29
|
+
if (!API_KEY) {
|
|
30
|
+
log('SEARCH1API_KEY not found. Stdio mode requires it; HTTP mode uses per-session keys.');
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
log('API key located successfully.');
|
|
7
34
|
}
|
|
8
35
|
// API configuration
|
|
9
36
|
export const API_CONFIG = {
|
package/build/http.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import express from "express";
|
|
4
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
5
|
+
import { createMcpServer } from "./server.js";
|
|
6
|
+
import { log } from "./utils.js";
|
|
7
|
+
const app = express();
|
|
8
|
+
app.use(express.json());
|
|
9
|
+
// Session storage: sessionId → { transport, server }
|
|
10
|
+
const sessions = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Extract API key from request: Authorization header > URL query parameter
|
|
13
|
+
*/
|
|
14
|
+
function extractApiKey(req) {
|
|
15
|
+
// 1. Authorization: Bearer <key>
|
|
16
|
+
const auth = req.headers.authorization;
|
|
17
|
+
if (auth?.startsWith("Bearer ")) {
|
|
18
|
+
return auth.slice(7).trim() || undefined;
|
|
19
|
+
}
|
|
20
|
+
// 2. ?apiKey=<key>
|
|
21
|
+
const queryKey = req.query.apiKey;
|
|
22
|
+
if (typeof queryKey === "string" && queryKey.trim()) {
|
|
23
|
+
return queryKey.trim();
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if a JSON-RPC request body is an initialize request
|
|
29
|
+
*/
|
|
30
|
+
function isInitializeRequest(body) {
|
|
31
|
+
if (Array.isArray(body)) {
|
|
32
|
+
return body.some((msg) => typeof msg === "object" && msg !== null && msg.method === "initialize");
|
|
33
|
+
}
|
|
34
|
+
return typeof body === "object" && body !== null && body.method === "initialize";
|
|
35
|
+
}
|
|
36
|
+
// Handle POST /mcp — initialize or route to existing session
|
|
37
|
+
app.post("/mcp", async (req, res) => {
|
|
38
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
39
|
+
// Route to existing session
|
|
40
|
+
if (sessionId && sessions.has(sessionId)) {
|
|
41
|
+
const session = sessions.get(sessionId);
|
|
42
|
+
await session.transport.handleRequest(req, res, req.body);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
// New session — must be an initialize request
|
|
46
|
+
if (!sessionId && isInitializeRequest(req.body)) {
|
|
47
|
+
const apiKey = extractApiKey(req);
|
|
48
|
+
if (!apiKey) {
|
|
49
|
+
res.status(401).json({
|
|
50
|
+
jsonrpc: "2.0",
|
|
51
|
+
error: {
|
|
52
|
+
code: -32001,
|
|
53
|
+
message: "API key is required. Pass it via 'Authorization: Bearer <key>' header or '?apiKey=<key>' query parameter.",
|
|
54
|
+
},
|
|
55
|
+
id: null,
|
|
56
|
+
});
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const transport = new StreamableHTTPServerTransport({
|
|
60
|
+
sessionIdGenerator: () => randomUUID(),
|
|
61
|
+
onsessioninitialized: (sid) => {
|
|
62
|
+
sessions.set(sid, { transport, server });
|
|
63
|
+
log(`Session created: ${sid}`);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
transport.onclose = () => {
|
|
67
|
+
if (transport.sessionId) {
|
|
68
|
+
sessions.delete(transport.sessionId);
|
|
69
|
+
log(`Session closed: ${transport.sessionId}`);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const server = createMcpServer(apiKey);
|
|
73
|
+
await server.connect(transport);
|
|
74
|
+
await transport.handleRequest(req, res, req.body);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
res.status(400).json({
|
|
78
|
+
jsonrpc: "2.0",
|
|
79
|
+
error: { code: -32600, message: "Bad request: no valid session or not an initialize request" },
|
|
80
|
+
id: null,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
// Handle GET /mcp — SSE stream for existing session
|
|
84
|
+
app.get("/mcp", async (req, res) => {
|
|
85
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
86
|
+
if (!sessionId || !sessions.has(sessionId)) {
|
|
87
|
+
res.status(400).json({
|
|
88
|
+
jsonrpc: "2.0",
|
|
89
|
+
error: { code: -32600, message: "Invalid or missing session ID" },
|
|
90
|
+
id: null,
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
await sessions.get(sessionId).transport.handleRequest(req, res);
|
|
95
|
+
});
|
|
96
|
+
// Handle DELETE /mcp — session termination
|
|
97
|
+
app.delete("/mcp", async (req, res) => {
|
|
98
|
+
const sessionId = req.headers["mcp-session-id"];
|
|
99
|
+
if (sessionId && sessions.has(sessionId)) {
|
|
100
|
+
const session = sessions.get(sessionId);
|
|
101
|
+
await session.transport.handleRequest(req, res);
|
|
102
|
+
await session.server.close();
|
|
103
|
+
sessions.delete(sessionId);
|
|
104
|
+
log(`Session terminated: ${sessionId}`);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
res.status(400).json({
|
|
108
|
+
jsonrpc: "2.0",
|
|
109
|
+
error: { code: -32600, message: "Invalid or missing session ID" },
|
|
110
|
+
id: null,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const PORT = parseInt(process.env.PORT || "3000", 10);
|
|
115
|
+
app.listen(PORT, () => {
|
|
116
|
+
log(`Search1API MCP HTTP server listening on http://localhost:${PORT}/mcp`);
|
|
117
|
+
});
|
|
118
|
+
// Handle global errors
|
|
119
|
+
process.on("uncaughtException", (error) => {
|
|
120
|
+
log("Uncaught exception:", error);
|
|
121
|
+
});
|
|
122
|
+
process.on("unhandledRejection", (reason) => {
|
|
123
|
+
log("Unhandled rejection:", reason);
|
|
124
|
+
});
|
package/build/index.js
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
2
3
|
import { log } from "./utils.js";
|
|
3
|
-
import {
|
|
4
|
+
import { createMcpServer } from "./server.js";
|
|
4
5
|
import { API_KEY } from "./config.js";
|
|
5
6
|
/**
|
|
6
|
-
* Main function - Program entry point
|
|
7
|
+
* Main function - Program entry point (stdio mode)
|
|
7
8
|
*/
|
|
8
9
|
async function main() {
|
|
9
|
-
//
|
|
10
|
+
// Stdio mode requires API key at startup
|
|
10
11
|
if (!API_KEY) {
|
|
11
12
|
log("SEARCH1API_KEY environment variable is not set");
|
|
12
13
|
process.exit(1);
|
|
13
14
|
}
|
|
14
15
|
try {
|
|
15
|
-
log("Starting Search1API MCP server");
|
|
16
|
-
const server =
|
|
17
|
-
|
|
18
|
-
await server.
|
|
16
|
+
log("Starting Search1API MCP server (stdio mode)");
|
|
17
|
+
const server = createMcpServer();
|
|
18
|
+
const transport = new StdioServerTransport();
|
|
19
|
+
await server.connect(transport);
|
|
19
20
|
log("Server started successfully");
|
|
20
21
|
// Handle process exit signals
|
|
21
|
-
|
|
22
|
+
const exitHandler = async () => {
|
|
23
|
+
log("Shutting down server...");
|
|
24
|
+
await server.close();
|
|
25
|
+
process.exit(0);
|
|
26
|
+
};
|
|
27
|
+
process.on("SIGINT", exitHandler);
|
|
28
|
+
process.on("SIGTERM", exitHandler);
|
|
29
|
+
process.on("SIGUSR1", exitHandler);
|
|
30
|
+
process.on("SIGUSR2", exitHandler);
|
|
22
31
|
}
|
|
23
32
|
catch (error) {
|
|
24
33
|
log("Failed to start server:", error);
|
|
25
34
|
process.exit(1);
|
|
26
35
|
}
|
|
27
36
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
process.exit(0);
|
|
36
|
-
};
|
|
37
|
-
// Handle various exit signals
|
|
38
|
-
process.on("SIGINT", exitHandler);
|
|
39
|
-
process.on("SIGTERM", exitHandler);
|
|
40
|
-
process.on("SIGUSR1", exitHandler);
|
|
41
|
-
process.on("SIGUSR2", exitHandler);
|
|
42
|
-
}
|
|
37
|
+
// Handle global errors
|
|
38
|
+
process.on("uncaughtException", (error) => {
|
|
39
|
+
log("Uncaught exception:", error);
|
|
40
|
+
});
|
|
41
|
+
process.on("unhandledRejection", (reason) => {
|
|
42
|
+
log("Unhandled rejection:", reason);
|
|
43
|
+
});
|
|
43
44
|
// Start program
|
|
44
45
|
main().catch((error) => {
|
|
45
46
|
log("Fatal error:", error);
|
package/build/server.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
2
|
import { McpError, ErrorCode, CallToolRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
4
3
|
import { handleToolCall } from "./tools/handlers.js";
|
|
5
4
|
import { log, formatError } from "./utils.js";
|
|
@@ -7,10 +6,11 @@ import { handleListResources, handleReadResource } from "./resources.js";
|
|
|
7
6
|
import { ALL_TOOLS } from "./tools/index.js";
|
|
8
7
|
/**
|
|
9
8
|
* Create and configure MCP server
|
|
9
|
+
* @param apiKey Optional per-session API key (used in HTTP mode)
|
|
10
|
+
* @returns Server instance ready to be connected to a transport
|
|
10
11
|
*/
|
|
11
|
-
export function
|
|
12
|
+
export function createMcpServer(apiKey) {
|
|
12
13
|
log("Creating Search1API MCP server");
|
|
13
|
-
// Create server instance
|
|
14
14
|
const server = new Server({
|
|
15
15
|
name: "search1api-server",
|
|
16
16
|
version: "1.0.0"
|
|
@@ -20,31 +20,8 @@ export function createServer() {
|
|
|
20
20
|
tools: {}
|
|
21
21
|
}
|
|
22
22
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Create STDIO transport
|
|
26
|
-
const transport = new StdioServerTransport();
|
|
27
|
-
return {
|
|
28
|
-
start: async () => {
|
|
29
|
-
try {
|
|
30
|
-
await server.connect(transport);
|
|
31
|
-
log("Server started successfully");
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
log("Failed to start server:", error);
|
|
35
|
-
throw error;
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
stop: async () => {
|
|
39
|
-
try {
|
|
40
|
-
await server.close();
|
|
41
|
-
log("Server stopped");
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
log("Error stopping server:", error);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
23
|
+
setupRequestHandlers(server, apiKey);
|
|
24
|
+
return server;
|
|
48
25
|
}
|
|
49
26
|
/**
|
|
50
27
|
* Helper function to handle errors uniformly
|
|
@@ -59,14 +36,14 @@ function handleError(context, error) {
|
|
|
59
36
|
/**
|
|
60
37
|
* Set up server request handlers
|
|
61
38
|
*/
|
|
62
|
-
function setupRequestHandlers(server) {
|
|
39
|
+
function setupRequestHandlers(server, apiKey) {
|
|
63
40
|
// Handle tool calls
|
|
64
41
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
65
42
|
try {
|
|
66
43
|
const toolName = request.params.name;
|
|
67
44
|
const toolArgs = request.params.arguments;
|
|
68
45
|
log(`Tool call received: ${toolName}`);
|
|
69
|
-
return await handleToolCall(toolName, toolArgs);
|
|
46
|
+
return await handleToolCall(toolName, toolArgs, apiKey);
|
|
70
47
|
}
|
|
71
48
|
catch (error) {
|
|
72
49
|
handleError("handling tool call", error);
|
|
@@ -102,11 +79,4 @@ function setupRequestHandlers(server) {
|
|
|
102
79
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
103
80
|
return { tools: ALL_TOOLS };
|
|
104
81
|
});
|
|
105
|
-
// Handle global errors
|
|
106
|
-
process.on("uncaughtException", (error) => {
|
|
107
|
-
log("Uncaught exception:", error);
|
|
108
|
-
});
|
|
109
|
-
process.on("unhandledRejection", (reason) => {
|
|
110
|
-
log("Unhandled rejection:", reason);
|
|
111
|
-
});
|
|
112
82
|
}
|
package/build/tools/crawl.js
CHANGED
|
@@ -6,7 +6,7 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the crawl tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleCrawl(args) {
|
|
9
|
+
export async function handleCrawl(args, apiKey) {
|
|
10
10
|
if (!isValidCrawlArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid crawl arguments");
|
|
12
12
|
}
|
|
@@ -14,7 +14,7 @@ export async function handleCrawl(args) {
|
|
|
14
14
|
log("Starting crawl for:", url);
|
|
15
15
|
try {
|
|
16
16
|
const startTime = Date.now();
|
|
17
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.CRAWL, { url });
|
|
17
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.CRAWL, { url }, apiKey);
|
|
18
18
|
const endTime = Date.now();
|
|
19
19
|
log(`Crawl completed successfully in ${endTime - startTime}ms`);
|
|
20
20
|
return {
|
package/build/tools/handlers.js
CHANGED
|
@@ -11,23 +11,24 @@ import { SEARCH_TOOL, CRAWL_TOOL, SITEMAP_TOOL, NEWS_TOOL, REASONING_TOOL, TREND
|
|
|
11
11
|
* Dispatch request based on tool name
|
|
12
12
|
* @param toolName Name of the tool
|
|
13
13
|
* @param args Tool parameters
|
|
14
|
+
* @param apiKey Optional per-session API key
|
|
14
15
|
* @returns Tool processing result
|
|
15
16
|
*/
|
|
16
|
-
export async function handleToolCall(toolName, args) {
|
|
17
|
+
export async function handleToolCall(toolName, args, apiKey) {
|
|
17
18
|
log(`Handling tool call: ${toolName}`);
|
|
18
19
|
switch (toolName) {
|
|
19
20
|
case SEARCH_TOOL.name:
|
|
20
|
-
return await handleSearch(args);
|
|
21
|
+
return await handleSearch(args, apiKey);
|
|
21
22
|
case CRAWL_TOOL.name:
|
|
22
|
-
return await handleCrawl(args);
|
|
23
|
+
return await handleCrawl(args, apiKey);
|
|
23
24
|
case SITEMAP_TOOL.name:
|
|
24
|
-
return await handleSitemap(args);
|
|
25
|
+
return await handleSitemap(args, apiKey);
|
|
25
26
|
case NEWS_TOOL.name:
|
|
26
|
-
return await handleNews(args);
|
|
27
|
+
return await handleNews(args, apiKey);
|
|
27
28
|
case REASONING_TOOL.name:
|
|
28
|
-
return await handleReasoning(args);
|
|
29
|
+
return await handleReasoning(args, apiKey);
|
|
29
30
|
case TRENDING_TOOL.name:
|
|
30
|
-
return await handleTrending(args);
|
|
31
|
+
return await handleTrending(args, apiKey);
|
|
31
32
|
default:
|
|
32
33
|
log(`Unknown tool: ${toolName}`);
|
|
33
34
|
throw new McpError(ErrorCode.InvalidParams, `Unknown tool: ${toolName}`);
|
package/build/tools/news.js
CHANGED
|
@@ -6,13 +6,13 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the news search tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleNews(args) {
|
|
9
|
+
export async function handleNews(args, apiKey) {
|
|
10
10
|
if (!isValidNewsArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid news search arguments");
|
|
12
12
|
}
|
|
13
13
|
log("Processing news search with query:", args.query);
|
|
14
14
|
try {
|
|
15
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.NEWS, args);
|
|
15
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.NEWS, args, apiKey);
|
|
16
16
|
return {
|
|
17
17
|
content: [{
|
|
18
18
|
type: "text",
|
package/build/tools/reasoning.js
CHANGED
|
@@ -6,16 +6,13 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the deep thinking and complex reasoning tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleReasoning(args) {
|
|
9
|
+
export async function handleReasoning(args, apiKey) {
|
|
10
10
|
if (!isValidReasoningArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid reasoning arguments");
|
|
12
12
|
}
|
|
13
13
|
const { content } = args;
|
|
14
14
|
log("Processing reasoning request");
|
|
15
15
|
try {
|
|
16
|
-
/**
|
|
17
|
-
* Implementation of the deep thinking and complex reasoning tool
|
|
18
|
-
*/
|
|
19
16
|
// Convert to API required format
|
|
20
17
|
const apiRequestData = {
|
|
21
18
|
model: "deepseek-r1-70b-fast-online",
|
|
@@ -27,9 +24,7 @@ export async function handleReasoning(args) {
|
|
|
27
24
|
],
|
|
28
25
|
stream: false
|
|
29
26
|
};
|
|
30
|
-
|
|
31
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.REASONING, apiRequestData // Using converted format instead of raw args
|
|
32
|
-
);
|
|
27
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.REASONING, apiRequestData, apiKey);
|
|
33
28
|
// Directly use the correct structure of ReasoningResponse
|
|
34
29
|
const resultText = response.choices && response.choices.length > 0
|
|
35
30
|
? response.choices[0].message.content
|
package/build/tools/search.js
CHANGED
|
@@ -6,12 +6,12 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the search tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleSearch(args) {
|
|
9
|
+
export async function handleSearch(args, apiKey) {
|
|
10
10
|
if (!isValidSearchArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid search arguments");
|
|
12
12
|
}
|
|
13
13
|
try {
|
|
14
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.SEARCH, args);
|
|
14
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.SEARCH, args, apiKey);
|
|
15
15
|
return {
|
|
16
16
|
content: [{
|
|
17
17
|
type: "text",
|
package/build/tools/sitemap.js
CHANGED
|
@@ -6,12 +6,12 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the sitemap tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleSitemap(args) {
|
|
9
|
+
export async function handleSitemap(args, apiKey) {
|
|
10
10
|
if (!isValidSitemapArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid sitemap arguments");
|
|
12
12
|
}
|
|
13
13
|
try {
|
|
14
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.SITEMAP, args);
|
|
14
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.SITEMAP, args, apiKey);
|
|
15
15
|
return {
|
|
16
16
|
content: [{
|
|
17
17
|
type: "text",
|
package/build/tools/trending.js
CHANGED
|
@@ -6,12 +6,12 @@ import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Implementation of the trending tool
|
|
8
8
|
*/
|
|
9
|
-
export async function handleTrending(args) {
|
|
9
|
+
export async function handleTrending(args, apiKey) {
|
|
10
10
|
if (!isValidTrendingArgs(args)) {
|
|
11
11
|
throw new McpError(ErrorCode.InvalidParams, "Invalid trending arguments");
|
|
12
12
|
}
|
|
13
13
|
try {
|
|
14
|
-
const response = await makeRequest(API_CONFIG.ENDPOINTS.TRENDING, args);
|
|
14
|
+
const response = await makeRequest(API_CONFIG.ENDPOINTS.TRENDING, args, apiKey);
|
|
15
15
|
return {
|
|
16
16
|
content: [{
|
|
17
17
|
type: "text",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "search1api-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Model Context Protocol (MCP) server that provides search and crawl functionality using Search1API",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -11,17 +11,20 @@
|
|
|
11
11
|
"build"
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
|
-
"build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\"",
|
|
14
|
+
"build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\" && node -e \"if (!require('fs').existsSync('.env.example')) { require('fs').writeFileSync('.env.example', '# Search1API Configuration\\n# Get your API key from https://www.search1api.com/?utm_source=mcp\\nSEARCH1API_KEY=your_api_key_here\\n') }\"",
|
|
15
|
+
"start": "node build/http.js",
|
|
15
16
|
"prepare": "npm run build",
|
|
16
17
|
"watch": "tsc --watch",
|
|
17
|
-
"inspector": "npx @modelcontextprotocol/inspector build/index.js"
|
|
18
|
+
"inspector": "npx @modelcontextprotocol/inspector build/index.js",
|
|
19
|
+
"start:http": "node build/http.js"
|
|
18
20
|
},
|
|
19
21
|
"dependencies": {
|
|
20
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
21
|
-
"
|
|
22
|
-
"
|
|
22
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
23
|
+
"dotenv": "^16.4.5",
|
|
24
|
+
"express": "^4.21.0"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
27
|
+
"@types/express": "^5.0.0",
|
|
25
28
|
"@types/node": "^20.11.24",
|
|
26
29
|
"typescript": "^5.3.3"
|
|
27
30
|
},
|