Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/data/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ navigation_title: REST API
| Navigation search | Search scoped to navigation structure and page titles |
| Ask AI | Streaming endpoint for AI-powered question answering over documentation |
| Changes feed | Content change feed for tracking documentation updates |
| APIs and API operations | Search, read, and list the REST API operations that API Explorer publishes |

## APIs and API operations

The `apis` resource describes the REST APIs that API Explorer publishes and, under each API, its operations. It answers the same lookups as the `search_api_operations`, `get_api_operation`, and `list_api_operations` tools of the [MCP server](mcp/index.md), and serves HTTP clients such as the Elastic CLI's `elastic docs api` commands. All requests are `GET` under `/docs/_api/v1`, and responses are camelCase JSON.

Every operation request names its API, such as `elasticsearch` or `kibana`. Many operation ids and routes exist in several APIs: `elasticsearch` and `elasticsearch-serverless` share most of theirs. A lookup across APIs would have to pick one, so there is none. The MCP tools still search across APIs when no API is given.

`version` is `main` or a major such as `9`. Without it, a request reads the API's current version.

### APIs

| Request | Query parameters | Returns |
|---------|------------------|---------|
| `/apis` | None | `apis`, with each API's key, title, landing page, and versions |
| `/apis/{api}` | None | That API's key, title, landing page, and versions |
| `/apis/{api}/groups` | `version`<br>`include_deprecated` (default `false`) | The API's `groups`, each with its name and operation count. Counts follow the same deprecated filter as a group listing. |

### Operations of an API

`/apis/{api}/operations` takes at most one of `q` or `endpoint`. Without either, it lists the API's operations by path.

| Query parameter | Meaning |
|-----------------|---------|
| `q` | Words, a route such as `GET /_cluster/health`, or a field path. A trailing dot, as in `retriever.`, lists child fields. Results come in relevance order with a `score` and the `matchedFields`. |
| `endpoint` | A request such as `GET /my-index/_search`, with or without the method. Results are the operations that serve it, best route first. A route made only of parameters, such as `/{index}`, can serve several. |
| `version` | The API version |
| `group` | Lists one group. It doesn't combine with `q` or `endpoint`. |
| `include_deprecated` | Include deprecated operations in a search or listing. The default is `false`. |
| `view` | `basic` (the default) or `full`. `full` adds each result's `versions` and `fields`, and needs `endpoint`. |
| `include_body` | Adds each result's `body`. It needs `view=full`. |
| `page`, `size` | Page through a search or listing. `size` defaults to `10` and is at most `50`. |

The response has `totalHits` and `results`. Each result has its `operation`, plus the fields its filters add. A lookup that matches nothing returns an empty page, not `404`.

`/apis/{api}/operations/{operationId}` reads one operation, such as `/apis/elasticsearch/operations/cluster-health`. It takes `version` and `include_body` (default `false`). It returns the `operation`, its `versions`, every documented field in `fields`, `bodyLength`, and `body` only when `include_body=true`.

### Errors

A request these resources can't answer returns `{ "errorType": "...", "message": "..." }`. The status is `404` when `errorType` ends in `_not_found`: `operation_not_found`, `api_not_found`, or `group_not_found`. Invalid arguments return `400` with `invalid_arguments`.

## Ask AI provider

Expand Down
85 changes: 85 additions & 0 deletions src/api/Elastic.Documentation.Api/MappingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Elastic.Documentation.Api.AskAi;
using Elastic.Documentation.Api.PageFeedback;
using Elastic.Documentation.Search;
using Elastic.Documentation.Search.ApiReference;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -26,6 +27,7 @@ public static void MapElasticDocsApiEndpoints(this IEndpointRouteBuilder group)
MapFullSearch(group);
MapChanges(group);
MapPageFeedback(group);
MapApiOperations(group);
}

private static void MapAskAiEndpoint(IEndpointRouteBuilder group)
Expand Down Expand Up @@ -185,6 +187,89 @@ Cancel ctx
return Results.Ok(response);
});

// The APIs API Explorer publishes and, under each, its operations. An operation is always addressed inside its
// API: many operation ids and routes exist in several APIs (elasticsearch and elasticsearch-serverless share
// most), so a lookup across APIs could only guess. The MCP tools answer the same lookups.
private static void MapApiOperations(IEndpointRouteBuilder group)
{
var apis = group.MapGroup("/apis");
_ = apis.MapGet(
"/",
async (ApiReferenceService apiReference, Cancel ctx) => ToResult(await apiReference.ListAsync(null, null, null, ctx))
);
_ = apis.MapGet(
"/{api}",
async (string api, ApiReferenceService apiReference, Cancel ctx) => ToResult(await apiReference.GetApiAsync(api, ctx))
);
_ = apis.MapGet(
"/{api}/groups",
async (
string api,
[FromQuery(Name = "version")] string? version,
[FromQuery(Name = "include_deprecated")] bool? includeDeprecated,
ApiReferenceService apiReference,
Cancel ctx
) => ToResult(await apiReference.ListGroupsAsync(api, version, includeDeprecated ?? false, ctx))
);
_ = apis.MapGet(
"/{api}/operations",
async (
string api,
[FromQuery(Name = "version")] string? version,
[FromQuery(Name = "group")] string? apiGroupName,
[FromQuery(Name = "q")] string? query,
[FromQuery(Name = "endpoint")] string? endpoint,
[FromQuery(Name = "include_deprecated")] bool? includeDeprecated,
[FromQuery(Name = "view")] string? view,
[FromQuery(Name = "include_body")] bool? includeBody,
[FromQuery(Name = "page")] int? pageNumber,
[FromQuery(Name = "size")] int? pageSize,
ApiReferenceService apiReference,
Cancel ctx
) => ToResult(
await apiReference.QueryAsync(
new ApiOperationQuery
{
Api = api,
Version = version,
Group = apiGroupName,
Q = query,
Endpoint = endpoint,
IncludeDeprecated = includeDeprecated ?? false,
View = view,
IncludeBody = includeBody ?? false,
Page = pageNumber ?? 1,
Size = pageSize ?? 10
},
ctx
)
)
);
_ = apis.MapGet(
"/{api}/operations/{operationId}",
async (
string api,
string operationId,
[FromQuery(Name = "version")] string? version,
[FromQuery(Name = "include_body")] bool? includeBody,
ApiReferenceService apiReference,
Cancel ctx
) => ToResult(await apiReference.GetOperationAsync(api, operationId, version, includeBody ?? false, ctx))
);
}

private static IResult ToResult(ApiReferenceReply reply) => reply switch
{
ApiOperationSearchResponse r => Results.Ok(r),
ApiOperationCollectionResponse r => Results.Ok(r),
ApiSummary r => Results.Ok(r),
ApiOperationResponse r => Results.Ok(r),
ApiListResponse r => Results.Ok(r),
ApiOperationListResponse r => Results.Ok(r),
ApiReferenceRejection r when r.ErrorType.EndsWith("_not_found", StringComparison.Ordinal) => Results.NotFound(r),
ApiReferenceRejection r => Results.BadRequest(r)
};

private static void MapPageFeedback(IEndpointRouteBuilder group)
{
_ = group.MapPut("/page-feedback/{feedbackId:guid}", async (
Expand Down
7 changes: 7 additions & 0 deletions src/api/Elastic.Documentation.Api/SerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,12 @@ public record OutputMessage(string Role, MessagePart[] Parts, string FinishReaso
[JsonSerializable(typeof(FullSearchAggregations))]
[JsonSerializable(typeof(ChangesResponse))]
[JsonSerializable(typeof(ChangedPageDto))]
[JsonSerializable(typeof(Search.ApiReference.ApiOperationSearchResponse))]
[JsonSerializable(typeof(Search.ApiReference.ApiOperationResponse))]
[JsonSerializable(typeof(Search.ApiReference.ApiListResponse))]
[JsonSerializable(typeof(Search.ApiReference.ApiOperationCollectionResponse))]
[JsonSerializable(typeof(Search.ApiReference.ApiSummary))]
[JsonSerializable(typeof(Search.ApiReference.ApiOperationListResponse))]
[JsonSerializable(typeof(Search.ApiReference.ApiReferenceRejection))]
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
public partial class ApiJsonContext : JsonSerializerContext;
1 change: 1 addition & 0 deletions src/api/Elastic.Documentation.Api/ServicesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ private static void AddSearchServices(IServiceCollection services, AppEnv appEnv

// Use the shared search service for DI registration
_ = services.AddSearchServices();
_ = services.AddApiReferenceServices();
logger?.LogInformation("Full search service registered with hybrid RRF support");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Elastic.Clients.Elasticsearch;
using Elastic.Documentation.Search;
using Elastic.Documentation.Search.ApiReference;
using Elastic.Documentation.Search.Common;
using Elastic.Documentation.Search.Contract;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal static class McpFeatureModules
"Use {tool:list_api_operations} to browse the available APIs and their operations."
],
ToolType: typeof(ApiTools),
RegisterServices: services => _ = services.AddScoped<IApiGateway, ApiGateway>()
RegisterServices: services => _ = services.AddApiReferenceServices()
);

public static readonly McpFeatureModule Coherence = new(Name: "Coherence", Capability: "analyze", WhenToUse: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information

using System.Text.Json.Serialization;
using Elastic.Documentation.Mcp.Remote.Gateways;
using Elastic.Documentation.Search.ApiReference;

namespace Elastic.Documentation.Mcp.Remote.Responses;

Expand All @@ -13,10 +13,8 @@ public sealed record ErrorResponse(string Error, List<string>? Details = null);
public static class ResponseBody
{
/// <summary>The note a response carries when it leaves out a body the caller can still ask for.</summary>
public static string? Note(string? body, bool includeBody) =>
!includeBody && body is { Length: > 0 }
? "The body is omitted by default. Call this tool again with includeBody=true to read it."
: null;
public static string? Note(int bodyLength, bool includeBody) =>
!includeBody && bodyLength > 0 ? "The body is omitted by default. Call this tool again with includeBody=true to read it." : null;
}

// SemanticSearch response
Expand Down Expand Up @@ -174,60 +172,6 @@ public sealed record AiEnrichmentStatusDto
public required bool HasUseCases { get; init; }
}

// search_api_operations response
public sealed record ApiOperationSearchResponse
{
public required string Query { get; init; }
public required long TotalHits { get; init; }
public required IReadOnlyList<ApiOperationHit> Results { get; init; }
}

// get_api_operation response
public sealed record ApiOperationResponse
{
public required ApiOperationSummary Operation { get; init; }
public required string[] Versions { get; init; }
public required IReadOnlyList<ApiField> Fields { get; init; }
public string? AiShortSummary { get; init; }
public string? Body { get; init; }
public int BodyLength { get; init; }
public string? BodyNote { get; init; }
public required IReadOnlyList<ApiOperationSummary> OtherMatches { get; init; }
}

// list_api_operations response without an API key
public sealed record ApiListResponse
{
public required IReadOnlyList<ApiSummary> Apis { get; init; }
}

// list_api_operations response for one API
public sealed record ApiOperationListResponse
{
public required string Api { get; init; }
public string? Version { get; init; }
public required int TotalOperations { get; init; }
public required IReadOnlyList<ApiOperationGroupDto> Groups { get; init; }
public string? Note { get; init; }
}

public sealed record ApiOperationGroupDto
{
public required string Name { get; init; }
public required int Count { get; init; }
public IReadOnlyList<ApiOperationListItemDto>? Operations { get; init; }
}

public sealed record ApiOperationListItemDto
{
public required string Title { get; init; }
public required string Url { get; init; }
public required string Method { get; init; }
public required string Path { get; init; }
public required string OperationId { get; init; }
public bool Deprecated { get; init; }
}

[JsonSourceGenerationOptions(WriteIndented = false, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(ErrorResponse))]
[JsonSerializable(typeof(SemanticSearchResponse))]
Expand All @@ -248,6 +192,7 @@ public sealed record ApiOperationListItemDto
[JsonSerializable(typeof(ApiOperationSearchResponse))]
[JsonSerializable(typeof(ApiOperationResponse))]
[JsonSerializable(typeof(ApiListResponse))]
[JsonSerializable(typeof(ApiOperationCollectionResponse))]
[JsonSerializable(typeof(ApiOperationListResponse))]
[JsonSerializable(typeof(ApiOperationGroupDto))]
[JsonSerializable(typeof(ApiOperationListItemDto))]
Expand Down
Loading
Loading