SSE初步完成

main
bicijinlian 2 days ago
parent 8fefa5a51a
commit 165bf19f2a

@ -6,8 +6,15 @@ using System.Threading.Tasks;
namespace McpStudy.Core
{
[McpServerToolType]
public class EchoTools
{
[McpServerTool, Description("Echoes the input back to the client.")]
public static string Echo(string message)
{
return "hello " + message;
}
}
}

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace McpStudy.Core;
[McpServerToolType]
public static class TimeTools
public class TimeTools
{
[McpServerTool, Description("Get the current time for a city")]
public static string GetCurrentTime(string city)

@ -1,6 +1,13 @@
using System.Diagnostics;
using System.Threading.Tasks;
using ModelContextProtocol;
using ModelContextProtocol.Client;
using ModelContextProtocol.AspNetCore;
namespace McpStudy.McpClient;
internal class Program
{
@ -10,4 +17,31 @@ internal class Program
await Task.CompletedTask;
}
static async Task CallStdio()
{
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "Everything",
Command = "npx",
Arguments = ["-y", "@modelcontextprotocol/server-everything"],
});
var client = await McpClientFactory.CreateAsync(clientTransport);
// Print the list of tools available from the server.
foreach (var tool in await client.ListToolsAsync())
{
Console.WriteLine($"{tool.Name} ({tool.Description})");
}
// Execute a tool (this would normally be driven by LLM tool invocations).
var result = await client.CallToolAsync(
"echo",
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
cancellationToken: CancellationToken.None);
// echo always returns one and only one text content object
Console.WriteLine(result.Content.First(c => c.Type == "text").ToString());
}
}

@ -0,0 +1,19 @@
global using System.Linq;
global using System.ComponentModel;
global using System.Net.Http.Headers;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using ModelContextProtocol;
global using ModelContextProtocol.Protocol;
global using ModelContextProtocol.Client;
global using ModelContextProtocol.Server;
global using ModelContextProtocol.SemanticKernel;
global using ModelContextProtocol.AspNetCore;
global using ModelContextProtocolServer;
global using ModelContextProtocolServer.Sse;
global using ModelContextProtocolServer.Stdio;

@ -8,6 +8,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.6" />
<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.1" />
</ItemGroup>
<ItemGroup>

@ -1,6 +1,5 @@
namespace McpStudy.McpServerSSE;
namespace McpStudy.McpServerSSE
{
public class Program
{
public static void Main(string[] args)
@ -10,15 +9,28 @@ namespace McpStudy.McpServerSSE
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly(typeof(McpStudy.Core.TimeTools).Assembly)
.WithToolsFromAssembly(typeof(McpStudy.McpServerSSE.Program).Assembly);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
});
}
app.UseAuthorization();
@ -26,7 +38,8 @@ namespace McpStudy.McpServerSSE
app.MapControllers();
app.MapMcp();
app.Run();
}
}
}

@ -4,7 +4,8 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"applicationUrl": "http://localhost:5027",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

@ -1,5 +1,7 @@
global using System.Linq;
global using System.ComponentModel;
global using System.Net.Http;
global using System.Net.Http.Headers;
global using Microsoft.Net.Http.Headers;
global using Microsoft.Extensions.Hosting;

Loading…
Cancel
Save