一个框架让Python函数同时支持HTTP和MCP,省去重复开发。
HarnessAPI: A Skill-First Framework for Unified Streaming APIs and MCP Tools
- 以类型化技能文件为唯一源头,自动生成HTTP和MCP接口。
- 实测减少74%代码冗余,支持流式与非流式客户端共用同一接口。
- 适合构建智能体工具的开发者,尤其关注降低维护成本者。
每个部署为大模型工具的Python函数目前需存在两种形式:供人工客户端和持续集成流水线使用的HTTP端点,以及供代理运行时(如Claude、Cursor)使用的MCP工具注册。两者共享业务逻辑,但周边机制(路由、验证、序列化、流式传输、模式维护)完全不同,随代码演进而逐渐偏离。我们提出HarnessAPI,一个以类型化技能文件为单一真实来源的Python框架。仅需一个handler.py加Pydantic模式,即可自动推导出支持服务器推送事件(SSE)的流式HTTP端点、交互式OpenAPI/Swagger UI,以及零配置的MCP工具,均由单个进程提供服务。双模式内容协商机制使同一处理器可同时服务SSE流式与返回JSON的客户端,无需修改。动态代码生成确保Pydantic类型注解正确传递至FastMCP的检查层,解决了朴素闭包注册无法实现的技术限制。在六个代表性技能上使用cloc测量,相比手动维护的双栈实现(FastAPI服务+FastMCP服务),HarnessAPI将框架相关样板代码减少74%。HarnessAPI继承FastAPI的完整中间件、依赖注入与部署生态,项目地址为https://github.com/edwinjosechittilappilly/harnessapi,可通过pip install harnessapi安装。
原文摘要 · Abstract (English)
Every Python function deployed as an LLM tool must today exist in two forms: an HTTP endpoint for human-facing clients and CI pipelines, and an MCP tool registration for agent runtimes such as Claude and Cursor. These representations share business logic yet diverge in all the surrounding machinery (routing, validation, serialisation, streaming, and schema maintenance), and they drift apart as the underlying code evolves. We present HarnessAPI, a Python framework that eliminates this duplication by treating a typed skill folder as the single source of truth. From one handler.py plus Pydantic schemas, the framework automatically derives a streaming HTTP endpoint with Server-Sent Events, an interactive OpenAPI/Swagger UI, and a zero-configuration MCP tool, all served from a single process. Dual-mode content negotiation lets the same handler serve SSE-streaming and JSON-returning clients with no handler changes. A dynamic code-generation mechanism ensures Pydantic type annotations propagate correctly to FastMCP's inspection layer, resolving a technical limitation that prevents naive closure-based registration. Measured across six representative skills using cloc, HarnessAPI reduces framework-facing boilerplate by 74% compared with a manually maintained dual-stack implementation (FastAPI server + FastMCP server). HarnessAPI subclasses FastAPI, inheriting its full middleware, dependency-injection, and deployment ecosystem. It is available at https://github.com/edwinjosechittilappilly/harnessapi and on PyPI (pip install harnessapi)
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。