mcpField
S
schema-discovery

schema-discovery

github.com/happy-technologies-llc数据与数据库4143 次安装2 天前更新
代码仓库

关于 schema-discovery

schema-discovery 是面向 ServiceNow 开发者、管理员和集成工程师的数据结构探索技能,涵盖字段、类型、文档及表关系,并介绍通过 MCP 工具或 REST API 获取信息。

主要功能

  1. 01按类别浏览数据表
  2. 02检查字段定义和类型
  3. 03梳理表继承与引用关系
  4. 04查询字段说明和选项值
  5. 05记录结构发现结果

适用场景

  1. 01编写查询前了解陌生数据表
  2. 02为集成追踪引用链
  3. 03核对选项值和字段要求

MCP 相关

介绍用于列出数据表、获取和探索结构、说明字段及查询数据表的 ServiceNow MCP 工具,也提供对应的 REST API 方法。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/happy-technologies-llc/happy-platform-skills --skill schema-discovery

安装后在 Claude Code 中输入/schema-discovery确认已加载。

详细介绍

架构发现

概述

架构发现是高效 ServiceNow 开发的基础。本技能将教你如何探索表结构、了解字段类型、发现表之间的关系,并记录发现结果以供开发使用。

  • 解决什么问题? 消除使用陌生表时的猜测,避免字段类型错误,并揭示 ServiceNow 实体之间隐藏的关系
  • 谁应该使用本技能? 在创建查询、脚本或集成之前,需要了解 ServiceNow 数据结构的开发人员、管理员和集成工程师
  • 预期成果是什么? 全面了解表架构、字段配置、引用关系以及字段的正确用法

前提条件

  • 所需的 ServiceNow 角色:admin 或 personalize_dictionary(用于完整的架构访问权限)
  • 对 sys_dictionary、sys_db_object 和 sys_documentation 表具有读取权限
  • 基本了解 ServiceNow 表层次结构和字段类型
  • 相关技能:无需其他技能(本技能为基础技能)

流程

步骤 1:发现可用表

首先,探索 ServiceNow 实例中有哪些可用表。表会根据其功能归类。

如果使用 MCP 工具:

Tool: SN-List-Available-Tables
Parameters:
  category: core_itsm

可用类别:

  • core_itsm - 事件、问题、变更、请求
  • cmdb - 配置管理数据库表
  • service_catalog - 服务目录项目、变量、请求
  • security - 用户、组、角色、ACL
  • system - 系统属性、计划作业、更新集
  • custom - 自定义应用表(x_* 前缀)

如果使用 REST API:

GET /api/now/table/sys_db_object?sysparm_query=super_class.name=task^ORsuper_class.name=cmdb_ci&sysparm_fields=name,label,super_class&sysparm_limit=50

决策点:

  • 如果要查找 ITSM 表 → 使用类别 core_itsm
  • 如果要探索 CMDB → 使用类别 cmdb
  • 如果要处理自定义应用 → 使用类别 custom,或按范围前缀查询
步骤 2:获取表架构

确定表后,检索其完整架构,包括所有字段、类型和配置。

如果使用 MCP 工具:

Tool: SN-Get-Table-Schema
Parameters:
  table_name: incident

如果使用 REST API:

GET /api/now/table/sys_dictionary?sysparm_query=name=incident^internal_type!=collection&sysparm_fields=element,column_label,internal_type,max_length,mandatory,reference

预期响应字段:

字段说明
element内部字段名称
column_label显示标签
internal_type字段类型(string、reference、integer 等)
max_length最大字符长度
mandatory字段是否必填
reference引用字段的目标表
default_value默认值(如有)
choice字段是否使用选项列表
步骤 3:通过关系深入发现架构

对于复杂集成,发现完整的关系映射,包括父表、子表和引用链。

如果使用 MCP 工具:

Tool: SN-Discover-Table-Schema
Parameters:
  table_name: incident
  include_relationships: true
  depth: 2

如果使用 REST API:

# Get table hierarchy (parent/child)
GET /api/now/table/sys_db_object?sysparm_query=name=incident&sysparm_fields=name,super_class,label

# Get all reference fields
GET /api/now/table/sys_dictionary?sysparm_query=name=incident^internal_type=reference&sysparm_fields=element,reference

# Get tables that reference this table
GET /api/now/table/sys_dictionary?sysparm_query=reference=incident&sysparm_fields=name,element

要发现的关系类型:

关系说明示例
继承父表层次结构incident 扩展 task
引用外键关系incident.assigned_to -> sys_user
Glide 列表多值引用incident.watch_list -> sys_user(列表)
文档 ID动态表引用sys_attachment.table_sys_id
步骤 4:了解字段类型

不同字段类型的行为、存储特性和查询语法各不相同。

常见的 ServiceNow 字段类型:

类型代码类型名称说明查询示例
string字符串文本,长度不超过 max_lengthshort_description=test
integer整数整数priority=1
boolean真/假布尔值active=true
reference引用指向另一张表的外键assigned_to.name=admin
glide_date日期仅日期due_date>2024-01-01
glide_date_time日期/时间日期和时间sys_created_on>=javascript:gs.daysAgo(7)
choice选项预定义选项state=1
journal日志仅追加的文本无法直接查询
journal_input日志输入工作备注/评论work_notesLIKEupdate
script脚本JavaScript 代码不适用
conditions条件编码查询不适用
步骤 5:获取字段文档

对于不熟悉的字段,请获取详细文档,其中包含用法示例和最佳实践。

如果使用 MCP 工具:

Tool: SN-Explain-Field
Parameters:
  table_name: incident
  field_name: impact

如果使用 REST API:

# Get field definition
GET /api/now/table/sys_dictionary?sysparm_query=name=incident^element=impact&sysparm_fields=element,column_label,internal_type,comments,hint

# Get field documentation
GET /api/now/table/sys_documentation?sysparm_query=name=incident^element=impact&sysparm_fields=text,short_description

文档包含:

  • 字段用途和业务含义
  • 有效值(针对选项字段)
  • 相关字段和依赖关系
  • 常见用例
  • API 行为说明
步骤 6:探索选项值

对于选项字段(下拉菜单),请查找所有有效值及其含义。

如果使用 MCP 工具:

Tool: SN-Query-Table
Parameters:
  table_name: sys_choice
  query: name=incident^element=state
  fields: value,label,sequence

如果使用 REST API:

GET /api/now/table/sys_choice?sysparm_query=name=incident^element=state&sysparm_fields=value,label,sequence&sysparm_orderby=sequence

常见选项表:

表用途
sys_choice标准字段选项
question_choice目录变量选项
sys_choice_set可重复使用的选项集
步骤 7:记录发现

为开发工作创建架构参考文档。

架构文档模板:

## Table: incident

### Hierarchy
- Parent: task
- Children: None

### Key Fields
| Field | Type | Required | Reference | Notes |
|-------|------|----------|-----------|-------|
| number | string | auto | - | Auto-generated |
| short_description | string | yes | - | Max 160 chars |
| assigned_to | reference | no | sys_user | Can be empty |
| state | integer | yes | - | Choice: 1-8 |

### State Values
| Value | Label |
|-------|-------|
| 1 | New |
| 2 | In Progress |
| 3 | On Hold |
| 6 | Resolved |
| 7 | Closed |
| 8 | Canceled |

### Key Relationships
- assignment_group -> sys_user_group
- caller_id -> sys_user
- cmdb_ci -> cmdb_ci
- problem_id -> problem

工具用法

MCP 工具(Claude Code/Desktop)
工具用途使用时机
SN-List-Available-Tables按类别列出表开始探索时
SN-Get-Table-Schema获取字段定义了解表结构时
SN-Discover-Table-Schema深入发现关系复杂集成时
SN-Explain-Field获取字段文档遇到不熟悉的字段时
SN-Query-Table查询 sys_choice、sys_dictionary自定义架构查询时
REST API(ChatGPT/其他)
端点方法用途
/api/now/table/sys_db_objectGET列出表
/api/now/table/sys_dictionaryGET获取字段定义
/api/now/table/sys_choiceGET获取选项值
/api/now/table/sys_documentationGET获取字段文档
/api/now/table/sys_glide_objectGET获取字段类型定义
原生工具(Claude Code)
工具用途
Bash执行 curl 命令以调用 REST API
Read读取缓存的架构文件

最佳实践

  • 缓存架构信息: 架构很少变化——在本地缓存结果,以减少 API 调用
  • 从父表开始: 先了解 task,再了解 incident;先了解 cmdb_ci,再了解 cmdb_ci_computer
  • 同时检查 sys_dictionary 和 sys_db_object: Dictionary 包含字段,db_object 包含表元数据
  • 记录引用链: incident -> assignment_group -> sys_user_group 有助于点遍历
  • 先在开发环境中验证: 由于自定义配置,不同实例的架构可能有所不同
  • 使用编码查询构建器: 在编写查询代码之前,先在 ServiceNow UI 中测试查询

故障排除

常见问题 1:架构中缺少字段

症状: 预期的字段未出现在架构结果中 原因: 字段可能位于父表中,而非子表中 解决方案: 查询父表,或使用带有 depth 参数的 SN-Discover-Table-Schema

Tool: SN-Discover-Table-Schema
Parameters:
  table_name: incident
  include_parent_fields: true
常见问题 2:对 sys_dictionary 的访问被拒绝

症状: 查询 sys_dictionary 时出现 403 错误 原因: 缺少 personalize_dictionary 或 admin 角色 解决方案: 申请相应角色,或使用针对特定表的端点

# Alternative: Use table API with schema
GET /api/now/table/incident?sysparm_limit=1&sysparm_display_value=all
# Response headers include field metadata
常见问题 3:引用字段显示 sys_id 而非值

症状: 引用字段返回的是 sys_id,而非显示值 原因: 未使用 sysparm_display_value 参数 解决方案: 在查询中添加 display_value 参数

Tool: SN-Query-Table
Parameters:
  table_name: incident
  display_value: all
常见问题 4:未找到选项值

症状: 对选项字段查询时,sys_choice 返回空结果 原因: 字段使用选项集,或定义方式不同 解决方案: 检查 sys_choice_set,或使用依赖值进行查询

Tool: SN-Query-Table
Parameters:
  table_name: sys_choice
  query: name=incident^element=subcategory^dependent_value=network

示例

示例 1:发现 CMDB 计算机架构
# Step 1: List CMDB tables
Tool: SN-List-Available-Tables
Parameters:
  category: cmdb

# Result: Lists cmdb_ci, cmdb_ci_computer, cmdb_ci_server, etc.

# Step 2: Get cmdb_ci_computer schema
Tool: SN-Get-Table-Schema
Parameters:
  table_name: cmdb_ci_computer

# Result:
Fields:
- cpu_core_count (integer)
- cpu_count (integer)
- cpu_speed (float)
- disk_space (float)
- ram (integer)
- os (string)
- os_version (string)
- serial_number (string)
- [inherited from cmdb_ci: name, ip_address, manufacturer, etc.]

# Step 3: Understand OS field
Tool: SN-Explain-Field
Parameters:
  table_name: cmdb_ci_computer
  field_name: os

# Result:
- Type: String
- Label: Operating System
- Description: Operating system name
- Common values: Windows, Linux, macOS
- Related field: os_version
示例 2:映射事件引用关系
# Discover all relationships from incident table
Tool: SN-Discover-Table-Schema
Parameters:
  table_name: incident
  include_relationships: true
  depth: 2

# Result:
Table: incident
Extends: task

Reference Fields:
- assigned_to -> sys_user
  - sys_user.manager -> sys_user (depth 2)
  - sys_user.department -> cmn_department (depth 2)
- assignment_group -> sys_user_group
  - sys_user_group.manager -> sys_user (depth 2)
  - sys_user_group.parent -> sys_user_group (depth 2)
- caller_id -> sys_user
- cmdb_ci -> cmdb_ci
  - cmdb_ci.location -> cmn_location (depth 2)
  - cmdb_ci.support_group -> sys_user_group (depth 2)
- problem_id -> problem
- rfc -> change_request

Reverse References (tables referencing incident):
- incident_sla.task -> incident
- incident_alert.incident -> incident
- sc_task.incident -> incident
示例 3:目录变量架构发现
# Step 1: Get catalog variable types
Tool: SN-Query-Table
Parameters:
  table_name: sys_choice
  query: name=item_option_new^element=type
  fields: value,label

# Result:
- 1: Yes/No
- 2: Multi Line Text
- 3: Multiple Choice
- 4: Numeric Scale
- 5: Select Box
- 6: Single Line Text
- 7: CheckBox
- 8: Reference
- 9: Date
- 10: Date/Time
- etc.

# Step 2: Understand variable definition structure
Tool: SN-Get-Table-Schema
Parameters:
  table_name: item_option_new

# Result:
- name (string): Variable name
- question_text (string): Label shown to user
- type (integer): Variable type from above
- reference (string): For type 8, target table
- cat_item (reference): Parent catalog item
- order (integer): Display sequence
- mandatory (boolean): Required field
示例 4:自定义应用表发现
# List custom tables in specific scope
Tool: SN-Query-Table
Parameters:
  table_name: sys_db_object
  query: nameSTARTSWITHx_myapp_
  fields: name,label,super_class

# Result:
- x_myapp_request (label: My App Request, extends: task)
- x_myapp_config (label: Configuration, extends: none)
- x_myapp_log (label: Activity Log, extends: none)

# Get schema for custom table
Tool: SN-Get-Table-Schema
Parameters:
  table_name: x_myapp_request

# Discover custom table relationships
Tool: SN-Discover-Table-Schema
Parameters:
  table_name: x_myapp_request
  include_relationships: true

相关技能

  • admin/update-set-management - 在开发过程中管理更新集
  • admin/batch-operations - 利用架构知识创建多条记录
  • development/business-rules - 编写使用已发现字段的脚本
  • development/scripted-rest-apis - 基于架构构建集成

参考资料

评论

支持 Markdown