Skip to content

Commit 30bc5df

Browse files
committed
fix: Fix MCP server handling for URI / direct json reference
1 parent facbd6c commit 30bc5df

File tree

8 files changed

+460
-124
lines changed

8 files changed

+460
-124
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ RUN dotnet publish "./src/OpenApi.Client.Mcp/OpenApi.Client.Mcp.csproj" -c Relea
1919

2020
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
2121
FROM base AS final
22+
EXPOSE 80
23+
EXPOSE 8080
24+
EXPOSE 8000
2225
WORKDIR /app
2326
COPY --from=publish /app/publish .
2427
ENTRYPOINT ["dotnet", "OpenApi.Client.Mcp.dll"]

README.md

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
11
# ☄️ OpenAPI Client
22

3-
[Created with ❤ in Poland by lepo.co](https://lepo.co/) and [wonderful open-source community](https://github.com/lepoco/openapi.client/graphs/contributors)
4-
OpenAPI Client is a toolkit that helps you create HTTP clients for external APIs based on their OpenAPI specifications. It simplifies the process of consuming and interacting with various web services. The project is developed and maintained by lepo.co and other community contributors.
3+
[Created with ❤ in Poland by lepo.co](https://lepo.co/) and [the awesome open-source community](https://github.com/lepoco/openapi.client/graphs/contributors).
4+
OpenAPI Client is a modern toolkit for developers working with OpenAPI/Swagger specifications. It provides multiple tools to simplify the creation, inspection, validation, and integration of OpenAPI clients across .NET projects and MCP (Model Context Protocol) environments.
55

6-
## 👀 What does this repo contain?
6+
## 🧰 What's in this repo?
77

8-
The repository contains NuGet package source code, which uses C# code generators that can be used to generate native C# API clients from YAML or JSON files.
8+
This repository contains a suite of OpenAPI tools designed to support different workflows:
99

10-
## MCP Server
10+
| Tool | Description |
11+
| ------------------------ | ------------------------------------------------------------------------------ |
12+
| 🧠 .NET Source Generator | Generates C# HTTP clients directly from OpenAPI JSON/YAML files at build time. |
13+
| 💻 .NET CLI Tool | Command-line tool to generate clients from OpenAPI definitions manually. |
14+
| 🛰️ MCP Server | Containerized server with built-in tools for working with OpenAPI specs. |
1115

12-
Build for yourself:
16+
## 🚀 MCP Server Tools
1317

14-
```cmd
15-
docker buildx build ./ -f ./src/OpenApi.Client.Mcp/Dockerfile -t mcp/openapi --no-cache
16-
```
18+
The MCP Server is the central piece of this toolkit. It runs as a standalone container or inside an MCP setup and exposes tools for working with OpenAPI documents.
19+
20+
### 🛠️ Available Server Tools
21+
22+
| Tool | Description |
23+
| ------------------------ | ------------------------------------------------------------------------------------------ |
24+
| `get_list_of_operations` | Lists all operation IDs from a given OpenAPI or Swagger JSON document. |
25+
| `get_known_responses` | Lists all known responses for given operation IDs from a OpenAPI or Swagger JSON document. |
26+
| `validate_document` | Validates the structure and syntax of an OpenAPI JSON document. |
27+
| `generate_curl_command` | Generates a cURL command for a specific operation ID. |
28+
| `create_csharp_snippet` | Creates a simple HTTP request for a given operation ID. |
29+
30+
### 🐳 Run the server with Docker
1731

18-
or
32+
Build the image:
1933

20-
```cmd
21-
dotnet publish .\src\OpenApi.Client.Mcp\OpenApi.Client.Mcp.csproj -c Release /t:PublishContainer
34+
```bash
35+
docker buildx build ./ -t mcp/openapi --no-cache
36+
# or
37+
dotnet publish ./src/OpenApi.Client.Mcp/OpenApi.Client.Mcp.csproj -c Release /t:PublishContainer
2238
```
2339

24-
Then
40+
Run the container:
2541

26-
```cmd
27-
docker run -d --name mcp-openapi mcp/openapi -e MODE=Stdio
28-
docker run -d --name mcp-openapi mcp/openapi -e MODE=Http -p 64622:8080
42+
```bash
43+
docker run -d -i --rm --name mcp-openapi mcp/openapi
44+
# or for HTTP mode:
45+
docker run -d -i --rm --name mcp-openapi mcp/openapi -e MODE=Http -p 64622:8080
2946
```
3047

31-
You can configure your *mcp.json* file
48+
Example MCP config (`.mcp.json`):
3249

3350
```json
3451
{
3552
"servers": {
36-
"openapi.mcp": {
53+
"openapi": {
3754
"type": "stdio",
3855
"command": "docker",
3956
"args": ["run", "-i", "--rm", "mcp/openapi"]
@@ -43,12 +60,12 @@ You can configure your *mcp.json* file
4360
}
4461
```
4562

46-
## Gettings started
63+
## 📦 NuGet Package (Source Generator)
4764

4865
OpenApiClient is available as NuGet package on NuGet.org:
4966
<https://www.nuget.org/packages/OpenApiClient>
5067

51-
You can add it to your project using .NET CLI:
68+
Install the package to enable automatic OpenAPI client generation:
5269

5370
```powershell
5471
dotnet add package OpenApiClient
@@ -60,82 +77,50 @@ dotnet add package OpenApiClient
6077
NuGet\Install-Package OpenApiClient
6178
```
6279

63-
, you can also use the .NET CLI tool to generate the classes using commands
64-
65-
```powershell
66-
dotnet tool install --global OpenApiClient.Cli
67-
```
68-
69-
Define an Open API file as content in your **.csproj** file.
80+
In your .csproj:
7081

7182
```xml
72-
<Project Sdk="Microsoft.NET.Sdk">
73-
74-
<PropertyGroup>
75-
<TargetFrameworks>net8.0</TargetFrameworks>
76-
</PropertyGroup>
77-
78-
<ItemGroup>
79-
<PackageReference Include="OpenApiClient" Version="1.0.0">
80-
<PrivateAssets>all</PrivateAssets>
81-
<IncludeAssets>compile; build; analyzers</IncludeAssets>
82-
</PackageReference>
83-
</ItemGroup>
84-
85-
<ItemGroup>
86-
<OpenApiContract Include="google.youtube.api.json" />
87-
</ItemGroup>
88-
89-
</Project>
83+
<ItemGroup>
84+
<AdditionalFiles Include="google.youtube.api.json" />
85+
</ItemGroup>
9086
```
9187

92-
Define your partial class as open api client
88+
Define a client:
9389

9490
```csharp
95-
/// <summary>
96-
/// My YouTube Client.
97-
/// </summary>
9891
[OpenApiClient("google.youtube.api")]
9992
public partial class YouTubeClient;
10093
```
10194

102-
You can now use your generated client!
95+
Use it:
10396

10497
```csharp
105-
IYouTubeClient client = new YouTubeClient(new HttpClient());
106-
107-
var subscribersCount = client.SubscribersCountAsync("mychannel", CancellationToken.None);
98+
var client = new YouTubeClient(new HttpClient());
99+
var subs = await client.SubscribersCountAsync("mychannel", CancellationToken.None);
108100
```
109101

110-
## Known limitations
102+
### Known limitations
111103

112104
Since we are using the generated internal `OpenApiAttribute` as a marker, conflicts may occur when we use `InternalsVisibleTo`.
113105

114106
We found the use of nullable essential, so C# 8.0 is required.
115107

116-
## OpenAPI
117-
118-
OpenAPI specification is available at:
119-
<https://github.com/OAI/OpenAPI-Specification>
120-
121-
## Community Toolkit
122-
123-
The OpenAPI Client is inspired by the MVVM Community Toolkit:
124-
<https://github.com/CommunityToolkit/dotnet>
125-
126-
## Special thanks
108+
## 💻 .NET CLI Tool
127109

128-
JetBrains was kind enough to lend a license for the open-source **dotUltimate** for Open API Client development. Learn more here:
110+
Generate OpenAPI clients from the terminal:
129111

130-
- https://www.jetbrains.com/dotnet/
131-
- https://www.jetbrains.com/opensource/
112+
```bash
113+
dotnet tool install --global OpenApiClient.Cli
114+
```
132115

133-
## Compilation
116+
```bash
117+
dotnet openapi generate ./google.youtube.api.json --output ./clients/YouTubeClient.cs --namespace Google.YouTube --classname YouTubeClient
118+
```
134119

135-
Use Visual Studio 2022 and invoke the .sln.
120+
## OpenAPI
136121

137-
Visual Studio
138-
**OpenAPI Client** is an Open Source project. You are entitled to download and use the freely available Visual Studio Community Edition to build, run or develop for OpenAPI Client. As per the Visual Studio Community Edition license, this applies regardless of whether you are an individual or a corporate user.
122+
OpenAPI specification is available at:
123+
<https://github.com/OAI/OpenAPI-Specification>
139124

140125
## Code of Conduct
141126

ThirdPartyNotices.txt

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,37 @@ Do Not Translate or Localize
55

66
This project incorporates components from the projects listed below. The original copyright notices and the licenses under which the .NET Foundation received such components are set forth below.
77

8-
1. CommunityToolkit/dotnet version 8.2.2 (https://github.com/CommunityToolkit/dotnet), included in all sources
9-
2. Sergio0694/ComputeSharp (https://github.com/Sergio0694/ComputeSharp), contributed by Sergio Pedri to reuse some helpers to support incremental generators in the MVVM Toolkit.
10-
3. Sergio0694/PolySharp (https://github.com/Sergio0694/PolySharp), used as inspiration for the implementation of the generators.
8+
1. Microsoft/OpenAPI.NET (https://github.com/Microsoft/OpenAPI.NET), used for interaction with OpenAPI documents.
9+
2. CommunityToolkit/dotnet version 8.2.2 (https://github.com/CommunityToolkit/dotnet), included in all sources
10+
3. Sergio0694/ComputeSharp (https://github.com/Sergio0694/ComputeSharp), contributed by Sergio Pedri to reuse some helpers to support incremental generators in the MVVM Toolkit.
11+
4. Sergio0694/PolySharp (https://github.com/Sergio0694/PolySharp), used as inspiration for the implementation of the generators.
12+
5. modelcontextprotocol/csharp-sdk (https://github.com/modelcontextprotocol/csharp-sdk), used as foundation of the MCP server.
13+
14+
%% Microsoft/OpenAPI.NET NOTICES AND INFORMATION BEGIN HERE
15+
=========================================
16+
Copyright (c) Microsoft Corporation. All rights reserved.
17+
18+
MIT License
19+
20+
Permission is hereby granted, free of charge, to any person obtaining a copy
21+
of this software and associated documentation files (the "Software"), to deal
22+
in the Software without restriction, including without limitation the rights
23+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24+
copies of the Software, and to permit persons to whom the Software is
25+
furnished to do so, subject to the following conditions:
26+
27+
The above copyright notice and this permission notice shall be included in all
28+
copies or substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
SOFTWARE.
37+
=========================================
38+
END OF Microsoft/OpenAPI.NET NOTICES AND INFORMATION
1139

1240
%% CommunityToolkit/dotnet NOTICES AND INFORMATION BEGIN HERE
1341
=========================================
@@ -90,3 +118,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
90118
SOFTWARE.
91119
=========================================
92120
END OF Sergio0694/PolySharp NOTICES AND INFORMATION
121+
122+
%% modelcontextprotocol/csharp-sdk NOTICES AND INFORMATION BEGIN HERE
123+
=========================================
124+
MIT License
125+
126+
Copyright (c) Anthropic and Contributors
127+
128+
Permission is hereby granted, free of charge, to any person obtaining a copy
129+
of this software and associated documentation files (the "Software"), to deal
130+
in the Software without restriction, including without limitation the rights
131+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
132+
copies of the Software, and to permit persons to whom the Software is
133+
furnished to do so, subject to the following conditions:
134+
135+
The above copyright notice and this permission notice shall be included in all
136+
copies or substantial portions of the Software.
137+
138+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
143+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
144+
SOFTWARE.
145+
=========================================
146+
END OF modelcontextprotocol/csharp-sdk NOTICES AND INFORMATION

src/OpenApi.Client.Mcp/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ RUN dotnet publish "./src/OpenApi.Client.Mcp/OpenApi.Client.Mcp.csproj" -c Relea
2020
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
2121
FROM base AS final
2222
WORKDIR /app
23+
EXPOSE 80
24+
EXPOSE 8080
25+
EXPOSE 8000
2326
COPY --from=publish /app/publish .
2427
ENTRYPOINT ["dotnet", "OpenApi.Client.Mcp.dll"]

src/OpenApi.Client.Mcp/Services/IOpenApiService.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,55 @@ namespace OpenApi.Client.Mcp.Services;
88
internal interface IOpenApiService
99
{
1010
/// <summary>
11-
/// Creates an OpenAPI client from the specified URL address of the OpenAPI JSON file or Swagger-generated JSON.
11+
/// Generates an OpenAPI client from a URL pointing to an OpenAPI or Swagger JSON document.
1212
/// </summary>
13-
Task<string> CreateFromFileAsync(string address, CancellationToken cancellationToken = default);
13+
Task<string> CreateCsharpClientAsync(
14+
string addressOrFileContents,
15+
CancellationToken cancellationToken = default
16+
);
17+
18+
/// <summary>
19+
/// Generates a C# code snippet for a given operation ID from the specified OpenAPI document or Swagger-generated JSON.
20+
/// </summary>
21+
Task<string> CreateCsharpSnippetAsync(
22+
string addressOrFileContents,
23+
string operationId,
24+
string? baseAddress,
25+
CancellationToken cancellationToken = default
26+
);
1427

1528
/// <summary>
16-
/// Retrieves a list of operations from the specified OpenAPI JSON file or Swagger-generated JSON.
29+
/// Retrieves a list of operations (endpoints) from the specified OpenAPI document or Swagger-generated JSON.
1730
/// </summary>
18-
Task<string> GetOperationsAsync(string address, CancellationToken cancellationToken = default);
31+
Task<string> GetOperationsAsync(
32+
string addressOrFileContents,
33+
CancellationToken cancellationToken = default
34+
);
1935

2036
/// <summary>
21-
/// Generates a curl command for a given operation ID from the OpenAPI specification.
37+
/// Validates the OpenAPI document or Swagger-generated JSON.
2238
/// </summary>
23-
Task<string> GenerateCurlCommandAsync(
24-
string address,
39+
Task<string> ValidateDocumentAsync(
40+
string addressOrFileContents,
41+
CancellationToken cancellationToken = default
42+
);
43+
44+
/// <summary>
45+
/// Retrieves known responses for the specified OpenAPI document or Swagger-generated JSON.
46+
/// </summary>
47+
Task<string> GetKnownResponsesAsync(
48+
string addressOrFileContents,
2549
string operationId,
26-
string? baseAddress,
2750
CancellationToken cancellationToken = default
2851
);
2952

3053
/// <summary>
31-
/// Validates the OpenAPI document at the specified address and returns a string indicating the validation result.
54+
/// Generates a cURL command for a given operation ID from the specified OpenAPI JSON file or Swagger-generated JSON.
3255
/// </summary>
33-
Task<string> ValidateDocumentAsync(string address, CancellationToken cancellationToken = default);
56+
Task<string> GenerateCurlCommandAsync(
57+
string addressOrFileContents,
58+
string operationId,
59+
string? baseAddress,
60+
CancellationToken cancellationToken = default
61+
);
3462
}

0 commit comments

Comments
 (0)