DhanHQ MCP服务器
一个模型上下文协议(MCP)服务器,提供对DhanHQ交易平台API的访问。此服务器允许AI助手和其他MCP客户端与您的DhanHQ交易账户进行交互。
特性
- 持股情况摘要:从DhanHQ获取您当前持有的资产
- 用于添加更多DhanHQ API端点的可扩展体系结构
先决条件
- Python 3.10或更高版本
- 具有API访问权限的DhanHQ帐户
- DhanHQ客户端ID和访问令牌
安装
选项1:从PyPI安装(发布时)
pip install dhan-mcp-server选项2:从源代码安装
git clone https://github.com/Vedhasagaran/dhan-mcp-py.git
cd dhan-mcp-py
pip install -e .选项3:从GitHub直接安装
pip install git+https://github.com/Vedhasagaran/dhan-mcp-py.git配置
1.设置环境变量
服务器要求将DhanHQ凭据设置为环境变量:
Windows(PowerShell):
$env:DHAN_CLIENT_ID="your_client_id"
$env:DHAN_ACCESS_TOKEN="your_access_token"Windows(命令提示符):
set DHAN_CLIENT_ID=your_client_id
set DHAN_ACCESS_TOKEN=your_access_tokenLinux/Mac:
export DHAN_CLIENT_ID="your_client_id"
export DHAN_ACCESS_TOKEN="your_access_token"使用.env文件(推荐):
创建一个 .env 项目目录中的文件:
DHAN_CLIENT_ID=your_client_id
DHAN_ACCESS_TOKEN=your_access_token2.配置MCP客户端
将服务器添加到MCP客户端配置中。配置文件位置因应用程序而异:
- 克劳德桌面:
%APPDATA%\Claude\claude_desktop_config.json(Windows)或~/Library/Application Support/Claude/claude_desktop_config.json(Mac) - 其他MCP客户端:请参阅客户的文件
配置示例:
{
"mcpServers": {
"dhan": {
"command": "dhan-mcp-server",
"env": {
"DHAN_CLIENT_ID": "your_client_id",
"DHAN_ACCESS_TOKEN": "your_access_token"
}
}
}
}直接使用Python的替代方案:
{
"mcpServers": {
"dhan": {
"command": "python",
"args": ["-m", "server"],
"env": {
"DHAN_CLIENT_ID": "your_client_id",
"DHAN_ACCESS_TOKEN": "your_access_token"
}
}
}
}用法
手动启动服务器
如果你想直接运行服务器:
dhan-mcp-server或者用Python:
python -m server服务器使用MCP协议通过stdio(标准输入/输出)进行通信。
可用工具
通过MCP客户端连接后,以下工具可用:
get_holdings_summary
从DhanHQ获取您当前持有的资产。
退货:
{
"holdings": [
// Array of holding objects
]
}get_all_orders
从您的DhanHQ帐户获取所有订单。
退货:
{
"orders": [
// Array of order objects with details like order ID, status, quantity, price, etc.
]
}发展
设置开发环境
# Clone the repository
git clone https://github.com/Vedhasagaran/dhan-mcp-py.git
cd dhan-mcp-py
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Install in editable mode with dependencies
pip install -e .添加新工具
要添加新的DhanHQ API端点,请执行以下操作:
- 在中添加新功能
server.py装饰有@mcp.tool() - 遵循以下模式:
@mcp.tool()
def your_new_tool() -> dict:
"""
Description of what this tool does.
"""
client = dhanhq(client_id, access_token)
result = client.your_api_method()
return {"data": result}安全说明
- 从不提交凭据:The
.gitignore文件排除.env文件 - 确保代币安全:访问令牌提供对您的DhanHQ帐户的完全访问权限
- 使用环境变量:始终从环境变量加载凭据
- 定期旋转令牌:遵循DhanHQ的安全最佳实践
故障排除
“缺少DHAN_CLIENT_ID或DHAN_ACCESS_TOKEN”
确保环境变量设置正确。请检查:
# Windows PowerShell
echo $env:DHAN_CLIENT_ID
# Linux/Mac
echo $DHAN_CLIENT_IDMCP客户端找不到服务器
验证安装:
pip show dhan-mcp-server
which dhan-mcp-server # Linux/Mac
where dhan-mcp-server # Windows许可证
MIT许可证-有关详细信息,请参阅许可证文件
贡献
欢迎投稿!请随时提交拉取请求。
