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55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# 使用官方Python基础镜像
FROM python:3.11-slim

# 安装系统依赖
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
    apt-get install -y --no-install-recommends git curl sed && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 安装 uv
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:${PATH}"

# 设置工作目录
WORKDIR /app

# 克隆项目源代码
RUN git clone https://github.com/openags/paper-search-mcp.git .

# 创建虚拟环境并安装依赖
RUN uv venv .venv
ENV PATH="/app/.venv/bin:${PATH}"

# 安装项目依赖
RUN uv pip install -e .

# 安装额外的web依赖
RUN uv pip install fastapi uvicorn python-multipart

# 创建一个优化的HTTP API包装器
COPY <<'EOF' /app/http_wrapper.py
#!/usr/bin/env python3

import asyncio
import json
import logging
import traceback
import os
from contextlib import asynccontextmanager
from typing import Any, Dict, Optional, List

import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel

# 设置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# 请求模型
class SearchRequest(BaseModel):
    query: str
    max_results: int = 10

class DownloadRequest(BaseModel):
    paper_id: str

# 全局变量
available_functions = {}
project_info = {}

async def explore_project_structure():
    """探索项目结构并找到可用的功能"""
    global available_functions, project_info
    
    try:
        # 尝试导入主模块
        import paper_search_mcp
        project_info['main_module'] = True
        logger.info("主模块导入成功")
        
        # 尝试导入服务器模块
        try:
            import paper_search_mcp.server as server_module
            project_info['server_module'] = dir(server_module)
            logger.info(f"服务器模块属性: {project_info['server_module']}")
            
            # 直接从server模块获取搜索函数
            search_functions = [
                'search_arxiv', 'search_pubmed', 'search_biorxiv', 
                'search_crossref', 'search_semantic', 'search_google_scholar',
                'search_iacr', 'search_medrxiv'
            ]
            
            for func_name in search_functions:
                if hasattr(server_module, func_name):
                    available_functions[func_name] = getattr(server_module, func_name)
                    logger.info(f"找到搜索函数: {func_name}")
                    
        except Exception as e:
            logger.error(f"服务器模块导入失败: {e}")
        
        # 尝试导入学术平台模块
        try:
            import paper_search_mcp.academic_platforms as platforms
            project_info['platforms_module'] = dir(platforms)
            logger.info(f"学术平台模块属性: {project_info['platforms_module']}")
                        
        except Exception as e:
            logger.error(f"学术平台模块导入失败: {e}")
            
        # 尝试导入各个子模块的搜索类
        submodules = ['arxiv', 'pubmed', 'biorxiv', 'crossref', 'semantic']
        for submodule in submodules:
            try:
                module = __import__(f'paper_search_mcp.academic_platforms.{submodule}', fromlist=[submodule])
                project_info[f'{submodule}_module'] = dir(module)
                logger.info(f"{submodule}模块属性: {project_info[f'{submodule}_module']}")
                
                # 查找搜索类
                for attr_name in dir(module):
                    if attr_name.endswith('Searcher') and not attr_name.startswith('_'):
                        SearcherClass = getattr(module, attr_name)
                        if callable(SearcherClass):
                            try:
                                # 创建搜索器实例
                                searcher = SearcherClass()
                                available_functions[f'{submodule}_searcher'] = searcher
                                logger.info(f"创建{submodule}搜索器实例: {attr_name}")
                            except Exception as e:
                                logger.warning(f"无法创建{submodule}搜索器实例: {e}")
                            
            except Exception as e:
                logger.warning(f"子模块 {submodule} 导入失败: {e}")
        
        logger.info(f"总共找到 {len(available_functions)} 个可用函数")
        
    except Exception as e:
        logger.error(f"项目探索失败: {e}")
        logger.error(traceback.format_exc())

@asynccontextmanager
async def lifespan(app: FastAPI):
    """应用生命周期管理"""
    logger.info("应用启动中...")
    await explore_project_structure()
    logger.info("应用启动完成!")
    yield
    logger.info("应用关闭中...")

# 创建FastAPI应用
app = FastAPI(
    title="Paper Search MCP Server",
    description="HTTP wrapper for Paper Search MCP Server with dynamic function discovery",
    version="1.0.0",
    lifespan=lifespan
)

@app.get("/", response_class=HTMLResponse)
async def root():
    return f"""
    <!DOCTYPE html>
    <html>
    <head>
        <title>Paper Search MCP Server</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            body {{ 
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; 
                margin: 0; 
                padding: 20px; 
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                min-height: 100vh;
                color: white;
            }}
            .container {{
                max-width: 1200px;
                margin: 0 auto;
                background: rgba(255,255,255,0.1);
                border-radius: 20px;
                padding: 30px;
                backdrop-filter: blur(10px);
                box-shadow: 0 8px 32px rgba(0,0,0,0.3);
            }}
            .header {{
                text-align: center;
                margin-bottom: 30px;
            }}
            .header h1 {{
                font-size: 2.5em;
                margin-bottom: 10px;
                background: linear-gradient(45deg, #fff, #f0f0f0);
                -webkit-background-clip: text;
                -webkit-text-fill-color: transparent;
            }}
            .info {{ 
                background: rgba(255,255,255,0.2); 
                padding: 20px; 
                margin: 20px 0; 
                border-radius: 15px; 
                border: 1px solid rgba(255,255,255,0.3);
            }}
            .endpoint {{ 
                background: rgba(255,255,255,0.15); 
                padding: 15px; 
                margin: 15px 0; 
                border-radius: 10px; 
                border-left: 4px solid #4CAF50;
                transition: all 0.3s ease;
            }}
            .endpoint:hover {{
                background: rgba(255,255,255,0.25);
                transform: translateX(5px);
            }}
            .method {{ 
                color: #4CAF50; 
                font-weight: bold; 
                padding: 4px 8px;
                background: rgba(76,175,80,0.2);
                border-radius: 4px;
                margin-right: 10px;
            }}
            .method.post {{ color: #FF9800; background: rgba(255,152,0,0.2); }}
            pre {{ 
                background: rgba(0,0,0,0.3); 
                padding: 15px; 
                border-radius: 8px; 
                overflow-x: auto; 
                font-family: 'Monaco', 'Menlo', monospace;
                border: 1px solid rgba(255,255,255,0.2);
            }}
            code {{
                background: rgba(255,255,255,0.2);
                padding: 2px 6px;
                border-radius: 4px;
                font-family: 'Monaco', 'Menlo', monospace;
            }}
            .status {{
                display: inline-block;
                padding: 5px 15px;
                background: #4CAF50;
                color: white;
                border-radius: 20px;
                font-size: 0.9em;
                margin-left: 10px;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>📚 Paper Search MCP Server</h1>
                <p>学术论文搜索与下载服务 <span class="status">🟢 Running</span></p>
            </div>
            
            <div class="info">
                <h3>🔍 发现的搜索功能 ({len(available_functions)} 个):</h3>
                <pre>{json.dumps(list(available_functions.keys()), indent=2)}</pre>
            </div>
            
            <!--
            <div class="info">
                <h3>📋 项目信息:</h3>
                <pre>{json.dumps(project_info, indent=2, default=str)}</pre>
            </div>
            -->
            
            <h2>🛠️ 可用API端点:</h2>
            
            <div class="endpoint">
                <span class="method">GET</span> <code>/health</code> - 健康检查与状态信息
            </div>
            
            <div class="endpoint">
                <span class="method">GET</span> <code>/functions</code> - 列出所有发现的功能
            </div>
            
            <div class="endpoint">
                <span class="method post">POST</span> <code>/search</code> - 通用搜索接口
                <br><small>📝 示例: {{"platform": "arxiv", "query": "machine learning", "max_results": 10}}</small>
            </div>
            
            <div class="endpoint">
                <span class="method post">POST</span> <code>/download</code> - 论文下载接口
                <br><small>📝 示例: {{"platform": "arxiv", "paper_id": "2301.12345"}}</small>
            </div>
            
            <div class="endpoint">
                <span class="method">GET</span> <code>/docs</code> - 📖 API文档 (Swagger UI)
            </div>
            
            <div class="endpoint">
                <span class="method">GET</span> <code>/redoc</code> - 📖 API文档 (ReDoc)
            </div>
        </div>
    </body>
    </html>
    """

@app.get("/health")
async def health():
    return {
        "status": "healthy", 
        "message": "Paper Search MCP Server is running",
        "functions_loaded": len(available_functions),
        "available_functions": list(available_functions.keys()),
        "uptime": "running"
    }

@app.get("/functions")
async def list_functions():
    return {
        "available_functions": list(available_functions.keys()),
        "project_info": project_info,
        "total_functions": len(available_functions)
    }

class GenericSearchRequest(BaseModel):
    platform: str
    query: str
    max_results: int = 10

@app.post("/search")
async def generic_search(request: GenericSearchRequest):
    try:
        # 查找合适的搜索函数
        possible_function_names = [
            f"search_{request.platform}",
            f"{request.platform}_search",
            f"{request.platform}_searcher"
        ]
        
        search_function = None
        used_function_name = None
        
        for func_name in possible_function_names:
            if func_name in available_functions:
                search_function = available_functions[func_name]
                used_function_name = func_name
                break
        
        if not search_function:
            return {
                "error": f"No search function found for platform: {request.platform}",
                "available_platforms": [name.replace('search_', '').replace('_searcher', '') for name in available_functions.keys()],
                "searched_for": possible_function_names
            }
        
        # 尝试调用搜索函数
        try:
            result = None
            error_msgs = []
            
            # 如果是搜索器实例,调用search方法
            if hasattr(search_function, 'search'):
                try:
                    result = await asyncio.to_thread(search_function.search, request.query, request.max_results)
                except Exception as e1:
                    error_msgs.append(f"搜索器实例调用失败: {str(e1)}")
            else:
                # 尝试直接调用函数
                try:
                    result = await asyncio.to_thread(search_function, request.query, request.max_results)
                except Exception as e2:
                    error_msgs.append(f"函数直接调用失败: {str(e2)}")
            
            if result is not None:
                return {
                    "platform": request.platform,
                    "function_used": used_function_name,
                    "query": request.query,
                    "results": result,
                    "count": len(result) if isinstance(result, (list, tuple)) else 1
                }
            else:
                return {
                    "error": "搜索调用失败",
                    "function_used": used_function_name,
                    "error_messages": error_msgs
                }
                
        except Exception as e:
            logger.error(f"搜索函数调用错误: {e}")
            return {
                "error": f"Function call failed: {str(e)}",
                "function_used": used_function_name
            }
            
    except Exception as e:
        logger.error(f"通用搜索错误: {e}")
        raise HTTPException(status_code=500, detail=str(e))

class GenericDownloadRequest(BaseModel):
    platform: str
    paper_id: str

@app.post("/download")
async def generic_download(request: GenericDownloadRequest):
    return {
        "message": "Download functionality is not implemented yet",
        "platform": request.platform,
        "paper_id": request.paper_id
    }

# 添加启动消息
@app.on_event("startup")
async def startup_event():
    logger.info("🚀 Paper Search MCP Server started successfully!")
    logger.info(f"📡 Server running on http://0.0.0.0:{os.getenv('PORT', '7860')}")

if __name__ == "__main__":
    # 获取端口,优先使用环境变量
    port = int(os.getenv("PORT", "7860"))
    host = "0.0.0.0"
    
    logger.info(f"🌟 Starting server on {host}:{port}")
    
    uvicorn.run(
        app, 
        host=host, 
        port=port, 
        log_level="info",
        access_log=True
    )
EOF

# ==============================================================================
# BEGIN PATCH: 修复 http_wrapper.py 中的异步调用错误
# ------------------------------------------------------------------------------
# 原因: 原始脚本错误地使用 asyncio.to_thread 来调用一个本身就是异步的函数。
#      这会导致返回一个未执行的协程对象,从而引发 FastAPI 的序列化错误。
# 解决方案: 我们使用 `sed` 命令在构建镜像时直接修改脚本,
#           将错误的 `asyncio.to_thread` 调用替换为正确的 `await` 调用。
# ==============================================================================
RUN \
    sed -i 's/result = await asyncio.to_thread(search_function.search, request.query, request.max_results)/result = await search_function.search(request.query, request.max_results)/g' /app/http_wrapper.py && \
    sed -i 's/result = await asyncio.to_thread(search_function, request.query, request.max_results)/result = await search_function(request.query, request.max_results)/g' /app/http_wrapper.py

# ==============================================================================
# END PATCH
# ==============================================================================

# 设置环境变量
ENV SEMANTIC_SCHOLAR_API_KEY=""
ENV PYTHONPATH=/app
ENV PORT=7860

# 声明端口 (Hugging Face Spaces 默认使用7860)
EXPOSE 7860

# 创建健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD curl -f http://localhost:7860/health || exit 1

# 启动HTTP包装器
CMD ["python", "/app/http_wrapper.py"]