UNPKG

@kpritam/gremlin-mcp

Version:

A Gremlin MCP server that allows for fetching status, schema, and querying using Gremlin for any Gremlin-compatible graph database (TypeScript implementation).

246 lines 8.63 kB
/** * Data Models Module for Gremlin Graph Database. * * This module defines the core data structures and types used throughout the Gremlin * graph database interface. It includes models for graph schema definitions and * knowledge graph components. Compatible with any Gremlin-enabled database including * Apache TinkerPop, Amazon Neptune, Azure Cosmos DB, etc. * * The models use Zod for clean, type-safe data structures that represent * both the graph structure and its contents with runtime validation. */ import { z } from 'zod'; /** * Represents a property definition for nodes and relationships in the graph. * * Properties are key-value pairs that can be attached to both nodes and * relationships, storing additional metadata about these graph elements. */ export const PropertySchema = z.object({ /** The name of the property */ name: z.string(), /** The data type(s) of the property */ type: z.array(z.string()), /** A list of sample values for the property (optional for schema size optimization) */ sample_values: z.array(z.unknown()).optional(), /** Cardinality information (single, list, set) */ cardinality: z.string().optional(), /** A list of all possible values, if the property is determined to be an enum */ enum: z.array(z.unknown()).optional(), }); /** * Defines a node type in the graph schema. * * Nodes represent entities in the graph database and can have labels * and properties that describe their characteristics. */ export const NodeSchema = z.object({ /** The label(s) that categorize this node type */ labels: z.string(), /** List of properties that can be assigned to this node type */ properties: z.array(PropertySchema).default([]), /** Count of vertices with this label */ count: z.number().optional(), }); /** * Defines a relationship type in the graph schema. * * Relationships represent connections between nodes in the graph and can * have their own properties to describe the nature of the connection. */ export const RelationshipSchema = z.object({ /** The type/category of the relationship */ type: z.string(), /** List of properties that can be assigned to this relationship type */ properties: z.array(PropertySchema).default([]), /** Count of edges with this label */ count: z.number().optional(), }); /** * Defines a valid relationship pattern between nodes in the graph. * * Relationship patterns describe the allowed connections between different * types of nodes in the graph schema. */ export const RelationshipPatternSchema = z.object({ /** The label of the source/starting node */ left_node: z.string(), /** The label of the target/ending node */ right_node: z.string(), /** The type of relationship connecting the nodes */ relation: z.string(), }); /** * Schema metadata for optimization information */ export const SchemaMetadataSchema = z.object({ /** Total size of the schema in bytes */ schema_size_bytes: z.number().optional(), /** Number of node types */ node_count: z.number(), /** Number of relationship types */ relationship_count: z.number(), /** Number of relationship patterns */ pattern_count: z.number(), /** Optimization settings used */ optimization_settings: z.object({ sample_values_included: z.boolean(), max_enum_values: z.number(), counts_included: z.boolean(), enum_cardinality_threshold: z.number(), }), /** When the schema was generated */ generated_at: z.string(), }); /** * Represents the complete schema definition for the graph database. * * The graph schema defines all possible node types, relationship types, * and valid patterns of connections between nodes. */ export const GraphSchemaSchema = z.object({ /** List of all node types defined in the schema */ nodes: z.array(NodeSchema), /** List of all relationship types defined in the schema */ relationships: z.array(RelationshipSchema), /** List of valid relationship patterns between nodes */ relationship_patterns: z.array(RelationshipPatternSchema), /** Schema metadata and optimization information */ metadata: SchemaMetadataSchema.optional(), }); /** * Gremlin configuration schema */ export const GremlinConfigSchema = z.object({ /** Host address of the Gremlin server */ host: z.string(), /** Port number of the Gremlin server */ port: z.number().int().positive(), /** Traversal source name */ traversalSource: z.string(), /** Whether to use SSL/TLS connection */ useSSL: z.boolean(), /** Optional username for authentication */ username: z.string().optional(), /** Optional password for authentication */ password: z.string().optional(), /** Idle timeout in seconds */ idleTimeoutSeconds: z.number().positive(), /** Whether enum discovery is enabled */ enumDiscoveryEnabled: z.boolean().optional().default(true), /** Cardinality threshold for enum discovery */ enumCardinalityThreshold: z.number().positive().optional().default(10), /** List of property names to exclude from enum discovery */ enumPropertyBlacklist: z.array(z.string()).optional().default([]), /** Whether to include sample values in schema (for size optimization) */ includeSampleValues: z.boolean().optional().default(false), /** Maximum number of enum values to include (for size optimization) */ maxEnumValues: z.number().positive().optional().default(10), /** Whether to include vertex/edge counts in schema */ includeCounts: z.boolean().optional().default(true), }); /** * Gremlin vertex with full structure */ export const GremlinVertexSchema = z.object({ id: z.unknown(), label: z.string(), properties: z.record(z.array(z.unknown())).optional(), type: z.literal('vertex'), }); /** * Gremlin edge with full structure */ export const GremlinEdgeSchema = z.object({ id: z.unknown(), label: z.string(), inV: z.unknown(), outV: z.unknown(), properties: z.record(z.array(z.unknown())).optional(), type: z.literal('edge'), }); /** * Gremlin property map (from valueMap() queries) */ export const GremlinPropertyMapSchema = z.record(z.array(z.unknown())); /** * Gremlin path result (from path() queries) */ export const GremlinPathSchema = z.object({ labels: z.array(z.string()), objects: z.array(z.unknown()), type: z.literal('path'), }); /** * Gremlin property result (individual Property/VertexProperty instances) */ export const GremlinPropertySchema = z.object({ key: z.string(), value: z.unknown(), type: z.literal('property'), }); /** * Discriminated union for structured Gremlin result types */ export const GremlinStructuredResultSchema = z.discriminatedUnion('type', [ GremlinVertexSchema, GremlinEdgeSchema, GremlinPathSchema, GremlinPropertySchema, ]); /** * Union type for all possible Gremlin result types (including primitives) */ export const GremlinResultItemSchema = z.union([ GremlinStructuredResultSchema, GremlinPropertyMapSchema, z.string(), z.number(), z.boolean(), z.null(), z.array(z.unknown()), z.record(z.unknown()), ]); /** * Gremlin query result structure with properly typed results */ export const GremlinQueryResultSchema = z.object({ /** Query results array with typed items */ results: z.array(GremlinResultItemSchema), /** Status message about the query execution */ message: z.string(), }); /** * Input schema for Gremlin query tool */ export const GremlinQueryInputSchema = z.object({ query: z .string() .min(1, 'Query cannot be empty') .describe('The Gremlin query to execute against the graph database'), }); /** * Input schema for import operations */ export const ImportDataInputSchema = z.object({ format: z.enum(['graphson', 'csv']), data: z.string().min(1, 'Data cannot be empty'), options: z .object({ clear_graph: z.boolean().optional(), batch_size: z.number().positive().optional(), validate_schema: z.boolean().optional(), }) .optional(), }); /** * Input schema for export operations */ export const ExportSubgraphInputSchema = z.object({ traversal_query: z.string().min(1, 'Traversal query cannot be empty'), format: z.enum(['graphson', 'json', 'csv']), include_properties: z.array(z.string()).optional(), exclude_properties: z.array(z.string()).optional(), max_depth: z.number().positive().optional(), }); //# sourceMappingURL=models.js.map