Skip to content

Add support to build the MCP SDK using Linux #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM swift:6.0-noble AS build

WORKDIR /app
COPY . ./
RUN ["swift", "package", "clean"]
CMD ["swift", "test", "--parallel"]
26 changes: 14 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
Expand All @@ -14,30 +12,34 @@ let package = Package(
.visionOS("1.0"),
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "MCP",
targets: ["MCP"])
.library(name: "MCP", targets: ["MCP"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-system.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log", from: "1.5.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "MCP",
dependencies: [
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "Logging", package: "swift-log"),
]),
.target(name: "libmcp"),
]
),
.target(
name: "libmcp",
dependencies: [

]
),
.testTarget(
name: "MCPTests",
dependencies: [
"MCP",
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "Logging", package: "swift-log"),
]),
]
),
]
)
7 changes: 3 additions & 4 deletions Sources/MCP/Base/Transports.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Darwin
import libmcp
import Logging

import struct Foundation.Data

#if canImport(System)
Expand Down Expand Up @@ -72,11 +71,11 @@ public actor StdioTransport: Transport {
}

private func setNonBlocking(fileDescriptor: FileDescriptor) throws {
let flags = fcntl(fileDescriptor.rawValue, F_GETFL)
let flags = fcntl_int(fileDescriptor.rawValue, F_GETFL)
guard flags >= 0 else {
throw MCPError.transportError(Errno.badFileDescriptor)
}
let result = fcntl(fileDescriptor.rawValue, F_SETFL, flags | O_NONBLOCK)
let result = fcntl_int_long(fileDescriptor.rawValue, F_SETFL, Int(flags | O_NONBLOCK))
guard result >= 0 else {
throw MCPError.transportError(Errno.badFileDescriptor)
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/libmcp/include/shims.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>

/* open functions */
int open_int(const char *path, int oflag);
int open_int_mode(const char *path, int oflag, mode_t mode);

/* fcntl functions */
int fcntl_int(int fildes, int cmd);
int fcntl_int_flock(int fildes, int cmd, struct flock* flock);
int fcntl_int_long(int fildes, int cmd, long arg);

/* ioctl functions */
int ioctl_long(int fd, unsigned long request);
int ioctl_long_void(int fd, unsigned long request, void* data);
5 changes: 5 additions & 0 deletions Sources/libmcp/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module libmcp [system] {
header "/usr/local/include/shims.h"
link "libmcp"
export *
}
35 changes: 35 additions & 0 deletions Sources/libmcp/shims.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "shims.h"

/* open functions */
int open_int(const char *path, int oflag) {
return open(path, oflag);
}
int open_int_mode(const char *path, int oflag, mode_t mode) {
return open(path, oflag, mode);
}

/* fcntl functions */
int fcntl_int(int fildes, int cmd) {
return fcntl(fildes, cmd);
}
int fcntl_int_flock(int fildes, int cmd, struct flock* flock) {
return fcntl(fildes, cmd, flock);
}
int fcntl_int_long(int fildes, int cmd, long arg) {
return fcntl(fildes, cmd, arg);
}

/* ioctl functions */
int ioctl_long(int fd, unsigned long request) {
return ioctl(fd, request);
}
int ioctl_long_void(int fd, unsigned long request, void* data) {
return ioctl(fd, request, data);
}
2 changes: 1 addition & 1 deletion Tests/MCPTests/RoundtripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct RoundtripTests {
let pingTask = Task {
try await client.ping()
// Ping doesn't return anything, so just getting here without throwing is success
#expect(true) // Test passed if we reach this point
#expect(Bool(true)) // Test passed if we reach this point
}

try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down