RSCG – RSCG_MCP2File

RSCG – RSCG_MCP2File
 
 

name RSCG_MCP2File
nuget https://www.nuget.org/packages/RSCG_MCP2File/
link https://github.com/ignatandrei/RSCG_OpenApi2MCP
author Ignat Andrei

Generating code for saving MCP result to file.

 

This is how you can use RSCG_MCP2File .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />

		<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
		<PackageReference Include="ModelContextProtocol" Version="0.5.0-preview.1" />
		<PackageReference Include="ModelContextProtocol.AspNetCore" Version="0.5.0-preview.1" />
		<PackageReference Include="RSCG_MCP2File" Version="9.2025.1202.1952" />
		<PackageReference Include="OpenAPISwaggerUI" Version="9.2024.1215.2209" />
		<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />

	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using MCPDemo;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenAPISwaggerUI;
using Serilog;
using Serilog.Events;

Console.WriteLine("Hello, World!");
var    builderApp = Host.CreateApplicationBuilder(args);

var    builderWeb = WebApplication.CreateBuilder();

// Configure all logs to go to stderr (stdout is used for the MCP protocol messages).
//builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);

var serverApp = builderApp.Services
    .AddMcpServer();
serverApp = serverApp.WithStdioServerTransport();
serverApp.WithTools<MyTools>();


var serverWeb = builderWeb.Services.AddMcpServer();
serverWeb = serverWeb.WithHttpTransport();
serverWeb.WithTools<MyTools>();



builderWeb.Services.AddOpenApi();
builderWeb.Services.AddTransient<MyTools>();
    
var app = builderApp.Build();
var web = builderWeb.Build();
    web.MapOpenApi();
    web.MapOpenApi("/openapi/{documentName}.yaml");
    web.MapMcp();
    web.UseOpenAPISwaggerUI();
    
    


var t1 = web.RunAsync();
var t2 = app.RunAsync();

await Task.WhenAll(t1, t2);



using ModelContextProtocol.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace MCPDemo;
[MCP2File.AddMCPExportToFile()]
partial class MyTools
{
    [McpServerTool]
    [Description("Echo demo")]
    public async Task<string> SendEcho([Description("echo")] string echoData)
    {
        await Task.Delay(10);
        return echoData;
    }

}


 

The code that is generated is


                namespace MCP2File
                {
                    [global::Microsoft.CodeAnalysis.EmbeddedAttribute]
                    [global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
                    internal class AddMCPExportToFile: global::System.Attribute {} 
                }
// <auto-generated/>
namespace Microsoft.CodeAnalysis
{
    internal sealed partial class EmbeddedAttribute : global::System.Attribute
    {
    }
}
// Auto-generated by MCP2File
namespace MCPDemo
{
    public partial class MyTools
    {
         [global::ModelContextProtocol.Server.McpServerTool]
               [global::System.ComponentModel.Description("calls the SendEcho and saves the result to a file ")]
        public async Task SendEchoExportToFile(string echoData, string exportToFile)
        {
            dynamic result = await SendEcho(echoData);
            if (result is byte[] bytes)
            {
                await File.WriteAllBytesAsync(exportToFile, bytes);
            }
            else if (result is string str)
            {
                await File.WriteAllTextAsync(exportToFile, str);
            }
            else
            {
                await File.WriteAllTextAsync(exportToFile, result?.ToString() ?? string.Empty);
            }
        }

    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_MCP2File


Posted

in

, ,

by

Tags: