LDAP查询服务器

用于AI聊天中查询LDAP目录的模型上下文协议(MCP)服务器,支持从Active Directory或其他LDAP服务器搜索用户、群组和组织数据。

  • by SCKelemen
  • Go
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 SCKelemen/ldap-mcp

使用说明

LDAP MCP服务器

用于在AI聊天中查询LDAP目录的模型上下文协议(MCP)服务器。使Claude和其他AI代理能够从Active Directory或其他LDAP服务器搜索用户、组和组织数据。

特性

MCP工具(共12个)

用户查询:

  • 搜索用户:通过电子邮件、用户名或姓名搜索用户
  • get_user_details:获取特定用户的全面信息
  • bulk_user_lookup:一次高效查询多个用户
  • get_direct_reports:让所有用户向经理报告

组查询:

  • 搜索组:按名称或描述查找组
  • get_group_members:列出组的所有成员
  • get_user_groups:获取用户所属的所有组
  • 验证会员资格:检查用户是否是组的成员

目录查询:

  • search_ou:搜索组织单位
  • get_计算机:查询AD中的计算机对象
  • search_by_attributes:灵活的多属性搜索
  • search_by_filter:执行自定义LDAP筛选器查询

性能特点:

  • 连接池(可配置)
  • TTL查询缓存(5-30分钟,取决于查询类型)
  • 速率限制(默认10 qps,突发20)

安装

从源代码构建

go build -o ldap-mcp ./cmd/ldap-mcp

配置

创建配置文件 .ldap-mcp.yaml:

ldap:
  server: ldap.example.com:389
  use_tls: false
  bind_dn: cn=serviceaccount,dc=example,dc=com
  bind_password: your-password
  base_dn: dc=example,dc=com
  timeout: 10s

或者使用环境变量:

export LDAP_SERVER=ldap.example.com:389
export LDAP_BIND_DN=cn=serviceaccount,dc=example,dc=com
export LDAP_BIND_PASSWORD=your-password
export LDAP_BASE_DN=dc=example,dc=com

Claude桌面配置

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "ldap": {
      "command": "ldap-mcp",
      "env": {
        "LDAP_SERVER": "ldap.example.com:389",
        "LDAP_BIND_DN": "cn=serviceaccount,dc=example,dc=com",
        "LDAP_BIND_PASSWORD": "your-password",
        "LDAP_BASE_DN": "dc=example,dc=com"
      }
    }
  }
}

Claude代码CLI配置

添加到您的 ~/.claude.json:

{
  "mcpServers": {
    "ldap": {
      "type": "stdio",
      "command": "/path/to/ldap-mcp",
      "args": [],
      "env": {
        "LDAP_SERVER": "ldap.example.com:389",
        "LDAP_BIND_DN": "cn=serviceaccount,dc=example,dc=com",
        "LDAP_BIND_PASSWORD": "your-password",
        "LDAP_BASE_DN": "dc=example,dc=com"
      }
    }
  }
}

用法示例

搜索用户

User: Find the user with email john.doe@example.com

Claude:
[Uses search_user tool]
Found user: John Doe (john.doe)
- Email: john.doe@example.com
- Display Name: John Doe
- Department: Engineering
- Status: Active

获取组成员

User: Who are the members of the Engineering team?

Claude:
[Uses get_group_members with group_identifier="Engineering"]
Engineering group has 15 members:
1. John Doe (john.doe@example.com)
2. Jane Smith (jane.smith@example.com)
...

注: 这 get_group_members 该工具接受简单的组名(例如“Engineering”)或完整的可分辨名称(例如“CN=Engineering,OU=Groups,DC=example,DC=com”)。

验证组成员身份

User: Is jane.smith a member of the Admins group?

Claude:
[Uses verify_membership tool]
Yes, jane.smith is a member of the Admins group.

高级示例

搜索组织单位

User: Show me all OUs in the London office

Claude:
[Uses search_ou tool with query="London"]
Found 3 organizational units:
1. OU=Users,OU=London,DC=midasplayer,DC=local
2. OU=Groups,OU=London,DC=midasplayer,DC=local
3. OU=Computers,OU=London,DC=midasplayer,DC=local

查询计算机对象

User: Find information about the computer named WS-ENG-001

Claude:
[Uses get_computer tool]
Computer: WS-ENG-001
- DNS Hostname: ws-eng-001.example.com
- Operating System: Windows 11 Enterprise
- OS Version: 10.0 (22631)
- Last Logon: 2026-01-25 14:32:15
- Status: Enabled

批量用户查找

User: Get information for users: john.doe, jane.smith, bob.jones

Claude:
[Uses bulk_user_lookup tool]
Found 3 users:
1. John Doe (john.doe@example.com) - Engineering
2. Jane Smith (jane.smith@example.com) - Product
3. Bob Jones (bob.jones@example.com) - Marketing

获取经理的直接下属

User: Who reports directly to sarah.manager?

Claude:
[Uses get_direct_reports tool]
Sarah Manager has 8 direct reports:
1. John Doe (john.doe@example.com) - Senior Engineer
2. Jane Smith (jane.smith@example.com) - Product Manager
3. Alice Brown (alice.brown@example.com) - Engineer
...

按自定义属性搜索

User: Find all users in the Security department who are in Stockholm

Claude:
[Uses search_by_attributes tool]
{
  "attributes": {
    "department": "Security",
    "l": "Stockholm"
  },
  "objectClass": "user"
}

Found 12 users matching criteria:
1. Samuel Kelemen (samuel.kelemen@king.com) - Principal Security Engineer
2. ...

性能特征

LDAP MCP包括高级性能功能:

连接池

  • 默认:最多10个连接,最少2个连接
  • 好处:减少延迟,提高吞吐量
  • 自动:健康检查和连接回收

缓存

  • 默认TTL:用户/组查询时间为5分钟
  • 演出:缓存查询速度提高1000000倍(1.8秒→ 1.7µs)
  • 智能:不同数据类型的不同TTL:
    • 用户/组查询:5分钟
    • 团体会员资格:10分钟(波动较小)
    • OU:30分钟(非常稳定)
    • 批量查询:2分钟(探索性)

速率限制

  • 默认:每秒10个查询,突发20个
  • 保护:防止LDAP服务器过载
  • 令牌桶:具有突发支持的平滑速率限制

配置

完整配置示例

.ldap-mcp.yaml:

ldap:
  server: ldap.example.com:389
  use_tls: true
  bind_dn: cn=serviceaccount,dc=example,dc=com
  bind_password: ${LDAP_PASSWORD}
  base_dn: dc=example,dc=com
  timeout: 10s

自定义池配置

poolConfig := ldap.PoolConfig{
    MaxConns:    20,              // Max concurrent connections
    MinConns:    5,               // Minimum idle connections
    MaxIdleTime: 10 * time.Minute, // Idle timeout
    DialTimeout: 10 * time.Second, // Connection timeout
    HealthCheck: 30 * time.Second, // Health check interval
}

自定义缓存配置

cacheConfig := ldap.CacheConfig{
    Enabled:         true,
    DefaultTTL:      5 * time.Minute,  // Default cache TTL
    CleanupInterval: 1 * time.Minute,   // Cleanup frequency
    MaxEntries:      1000,              // Max cache entries
}

自定义速率限制配置

rateLimitConfig := ldap.RateLimitConfig{
    Enabled:       true,
    QueriesPerSec: 10.0, // Queries per second
    BurstSize:     20,   // Burst capacity
}

建筑

ldap-mcp/
├── cmd/
│   └── ldap-mcp/       # Main MCP server binary
├── internal/
│   └── ldap/           # LDAP connection and query logic
├── mcp/                # MCP server and tool implementations
├── .ldap-mcp.yaml      # Configuration file (not in repo)
└── go.mod

设计原则

遵循MCP最佳实践:

  • 特定数据源:专门用于LDAP查询
  • 有状态的连接:维护LDAP连接池
  • 配置驱动:灵活的连接设置
  • 安全意识:支持TLS、凭据管理
  • 可组合:与其他MCP服务器配合使用

工具详细信息

搜索用户

按各种属性搜索用户。

参数:

  • query (字符串,必填):搜索词(电子邮件、用户名或显示名称)
  • attributes ([]string,可选):要返回的其他属性

退货: 具有基本信息的匹配用户列表

get_user_details

获取特定用户的全面信息。

参数:

  • identifier (字符串,必填):用户电子邮件、用户名或DN

退货: 具有所有可用属性的完整用户对象

搜索组

按名称或描述搜索组。

参数:

  • query (string,必填):组名或描述子字符串

退货: 匹配组列表

get_group_members

列出组的所有成员。

参数:

  • group_identifier (字符串,必填):组名(CN)或全名(DN)

退货: 所有组成员的用户对象列表

示例:

// Using group name
{
  "group_identifier": "Engineering"
}

// Using full DN
{
  "group_identifier": "CN=Engineering,OU=Groups,DC=example,DC=com"
}

验证会员资格

检查用户是否是组的成员。

参数:

  • user_identifier (字符串,必填):用户电子邮件、用户名或DN
  • group_identifier (字符串,必填):组名或DN

退货: 表示成员资格状态的布尔值

search_by_filter

执行自定义LDAP筛选器查询。

参数:

  • filter (字符串,必填):LDAP筛选器(例如,“(objectClass=person)”)
  • base_dn (字符串,可选):搜索的基本DN(默认为config)
  • attributes ([]string,可选):要返回的属性

退货: 匹配的LDAP条目列表

get_user_groups

获取用户所属的所有组。

参数:

  • user_identifier (字符串,必填):用户电子邮件、用户名或DN

退货: 组名和DN列表

search_ou

在目录中搜索组织单位。

参数:

  • query (string,必填):要搜索的OU名称或子字符串

退货: 带位置信息的OU列表(城市、州、国家)

例子:

{
  "query": "London"
}

get_计算机

查询Active Directory中的计算机对象。

参数:

  • name (字符串,必填):计算机名(CN或主机名)

退货: 带有系统信息(操作系统、版本、上次登录)的计算机对象

例子:

{
  "name": "WS-ENG-001"
}

bulk_user_lookup

在单个请求中高效地查询多个用户。

参数:

  • identifiers ([]字符串,必填):用户电子邮件、用户名或DN列表

退货: 所有已找到用户的用户对象列表

例子:

{
  "identifiers": ["john.doe", "jane.smith", "bob.jones"]
}

注: 使用优化的LDAP OR过滤器,比多个单独的查询具有更好的性能。

get_direct_reports

让所有直接向经理报告的用户。

参数:

  • manager_identifier (字符串,必填):经理的电子邮件、用户名或DN

退货: 直接报告用户对象列表

例子:

{
  "manager_identifier": "sarah.manager"
}

search_by_attributes

使用多个LDAP属性进行灵活搜索。

参数:

  • attributes (map[string]string,必填):属性名称和值的键值对
  • object_class (字符串,可选):LDAP对象类筛选器(默认值:“user”)

退货: 匹配的LDAP条目列表

例子:

{
  "attributes": {
    "department": "Engineering",
    "title": "Senior Engineer",
    "l": "Stockholm"
  },
  "object_class": "user"
}

注: 所有属性条件都是AND键合在一起的。使用 search_by_filter 对于更复杂的查询。

安全考虑

  1. 凭证:安全地存储绑定凭据(环境变量、机密管理器)
  2. 传输层安全:为生产环境启用TLS
  3. 最小特权:使用具有最小权限的只读服务帐户
  4. 速率限制:内置令牌桶速率限制器可防止LDAP服务器过载
    • 默认值:每秒10个查询,突发20个
    • 可根据部署进行配置
  5. 连接池:限制并发连接以防止资源耗尽
  6. 缓存:减少LDAP服务器上的负载并缩短响应时间
  7. 日志记录:审核LDAP查询以进行安全监控

与其他方法的比较

为什么选择MCP工具(不是技能)?

LDAP MCP是 工具 因为它:

  • ✅ 提供从特定源(LDAP)检索数据
  • ✅ 需要状态连接管理
  • ✅ 作为独立服务
  • ✅ 可与其他MCP组合(例如,与Jira、Slack MCP组合)

A. 技能 适用于:

  • ❌ 意见化的工作流程(例如,“提供新用户帐户”)
  • ❌ 多步骤编排(LDAP+票务+电子邮件)
  • ❌ Claude Code特定功能

发展

运行测试

go test ./...

在本地运行

export LDAP_SERVER=ldap.example.com:389
export LDAP_BIND_DN=cn=serviceaccount,dc=example,dc=com
export LDAP_BIND_PASSWORD=your-password
export LDAP_BASE_DN=dc=example,dc=com

./ldap-mcp

许可证

BearWare 1.0(MIT兼容)🐻

看 许可证 获取完整的熊框许可证文本。